Deserialization fails - asp.net

this is my code
DropDownList1.Items.Add(new ListItem(" Prueba1"));
DropDownList2.Items.Add(new ListItem(" Prueba1"));
//string[] filePaths = Directory.GetFiles(#"\\hmfsw\web\DTCWIN107\MYSITE.com\public_html\post\");
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo(#"\\hmfsw\web\DTCWIN107\MYSITE.com\public_html\post\");
string path = #"\\hmfsw\web\DTCWIN107\MYSITE.com\public_html\linker\linker.xml";
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles();
// Display the names of the files.
foreach (FileInfo fri in fiArr)DropDownList1.Items.Add(new ListItem(fri.Name));
System.Xml.Serialization.XmlSerializer serializer =
new System.Xml.Serialization.XmlSerializer(typeof(selector));
System.IO.TextReader reader =
new System.IO.StreamReader(path);
selector s = (selector)serializer.Deserialize(reader);
reader.Close();
OKay, The first thing is that the dropdownlist daes not update with prueba1 and prueba2
But the dropdownlist do update with info of the folder...
And the second thiing is that the serializer.Deserialize daes nothing. The dropdownlist is not been load with the xml ...
Could you plz help me?
Thanks

I don't see any association between Dropdownlist and the selector.

Related

How to get the most recent created subdirectory by using linq?

I am very new to Linq. But I am trying to write a function to get the most recent created subdirectory by using Directory.GetDirectories(rootPath).Where(...). Is it possible and how to I continue?
var info = new DirectoryInfo(rootPath);
var latestDirectory = info.GetDirectories()
.OrderByDescending(d => d.CreationTime)
.FirstOrDefault();
You can easily change name of DirectoryInfo class property you'd like to sort by.
DirectoryInfo.GetDirectories Method returns array of System.IO.DirectoryInfo, so you can easily order that directories by CreationTime and return the first one.
VB.NET solution:
Dim info As New DirectoryInfo(rootPath)
Dim latestDirectory As DirectoryInfo = info.GetDirectories() _
.OrderByDescending(Function(d) d.CreationTime) _
.FirstOrDefault()
You need to get specific DirectoryInfo information to determine the creation time. Try this.
DirectoryInfo di = new DirectoryInfo(rootPath);
var directory = di.GetDirectories()
.OrderByDescending(d => d.CreationTime)
.FirstOrDefault();

Force update file timestamp in VB.net

I am using the following code to copy a file:
System.IO.File.Copy(strOldFile, strNewFile)
But the trouble is that the newly-created file inherits the old file's timestamp. Is there a way to force the timestamp to update?
You can edit the CreationTime using the FileInfo class.
Dim path = Path.GetTempFileName();
Dim fi As New FileInfo(path)
fi.CreationTime = DateTime.Now;
fi.LastWriteTime = DateTime.Now;

The document has no pages

I am using the below code to export gridview to PDF
form1.Controls.Clear();
form1.Controls.Add(GridView1);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
string html = "<html><body>" + sw.ToString() + "</body></html>";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.AddAuthor("Ram");
document.AddSubject("Export To pdf");
document.Open();
string tempFile = Path.GetTempFileName();
using (StreamWriter tempwriter = new StreamWriter(tempFile, false))
{
tempwriter.Write(html);
}
HtmlParser.Parse(document, tempFile);
document.Close();
writer.Close();
File.Delete(tempFile);
writer = null;
document = null;
Response.End();
I have checked that grridview has 10 rows by putting breakpoint. But I am getting error at
document.Close();
that
The document has no pages.
Any suggestion how to fix it?
1) Setting a breakpoint to see that your grid view has 10 rows helps, but only checks part of the problem. You also need to check the contents of tempFile. That's what iText is actually working with. If it's empty, you'll sill get the "doc has no pages" exception.
2.1) HtmlParser no longer exists within iText. Having said that, I just dug up this code sample via google:
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("html1.pdf"));
HtmlParser.parse(document, "example.html");
}
No opens or closes, just a call to HtmlParser. It's quite possible that HtmlParser checks to see if the document is already open, and won't proceed if it is... that would explain the behavior you're seeing.
2.2) The "correct" way to convert HTML these days goes something like this:
String html = readHtml();
List<Element> objects = HTMLWorker.parseToList(new StringReader(html), null);
for (Element element : objects)
document.add(element);
I had same issue and the below suggestion help and my issue is solved
Document has no pages means your GridView data is lost when you export.
Hence in the Export button event rebind the GridView with data from database
https://www.aspforums.net/Threads/264988/Export-GridView-to-PDF-Error-Document-has-no-pages/

Programmatic update of InfoPath form XML is stripping off tags

I have a C# program that is looping through all of the forms (browser enabled) in my forms library and injecting an XML node into each of them (for a newly promoted field). For some reason, when the XML is saved back to the form, the first few tags are being stripped off. Specifically, these tags are:
<?xml version="1.0"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Contractor-DB-Form:-myXSD-2009-09-10T18-19-55" solutionVersion="1.0.1.1100" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://echouat.rbs.us/npe/FormServerTemplates/Contractor_DB_Form.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<?mso-infoPath-file-attachment-present?>"
My code to update the XML is as follows:
private static SPListItem InsertXmlNode(SPListItem infoPathForm, string nodeToUpdateStr, string nodeToInsertStr,
string nodeInnerXmlStr, string firstNode)
{
//load form into xml document
byte[] fileBytes = infoPathForm.File.OpenBinary();
MemoryStream itemStream = new MemoryStream(fileBytes);
//Stream itemStream = infoPathForm.File.OpenBinary();
XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager xmlNameSpaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNameSpaceMgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55");
xmlDoc.Load(itemStream);
itemStream.Close();
//inject xml
XmlNode nodeToUpdate = xmlDoc.SelectSingleNode(firstNode + nodeToUpdateStr, xmlNameSpaceMgr);
//only insert if doesn't already exist
if (xmlDoc.SelectSingleNode(firstNode + nodeToUpdateStr + "/" + nodeToInsertStr, xmlNameSpaceMgr) == null)
{
updateCounter++;
XmlNode nodeToInsert = xmlDoc.CreateNode(XmlNodeType.Element, nodeToInsertStr, "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-10T18:19:55");
nodeToInsert.InnerText = nodeInnerXmlStr;
nodeToUpdate.AppendChild(nodeToInsert);
//get binary data for updated xml
byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.DocumentElement.OuterXml);
MemoryStream newMemStream = new MemoryStream(newXmlData);
//write updated binary data to the form
infoPathForm.File.SaveBinary(newMemStream);
newMemStream.Close();
infoPathForm.File.Update();
}
return infoPathForm;
}
The addition of the new node is working properly; I can see that the new XML is properly formed. It's just that the tags get stripped off once the file is loaded from the MemoryStream into the XmlDocument object. And once these tags are missing, the forms won't open anymore in IP.
PLEASE HELP!
Thank you!
Change the line which reads:
byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.DocumentElement.OuterXml);
to read:
byte[] newXmlData = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);

System.InvalidOperationException: This document already has a 'DocumentElement' node

I have a trouble on appending new node to xmldocument (created in the memory). I have select the root node with the XmlDocument.SelectSingleNode() method, it work sometimes and in the other time it will give me "System.InvalidOperationException: This document already has a 'DocumentElement' node." error. More information, this xml document is multi level xml document.
By the way, when i try it with unit test it work fine (always), when i implement it in ASP.NET 3.5, it become weird, work sometimes and fail sometimes. Any idea, why this can help? All advise and suggestion are welcome.
Thanks.
You can select the root node of the XmlDocument using the Property "DocumentElement". Or i think you can use the Property "FirstChild" (untested).
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
XmlElement rootNode = doc.DocumentElement;
This worked for me.
xmlOriginFile = New XmlDocument()
xmlTargetFile = New XmlDocument()
xmlOriginFile.Load(readFile) //readFile is a string that hold path to xml document
xmlTargetFile.Load(writeFile) //writeFile is a string that hold path to xml document
Dim fileNav As XPathNavigator = xmlOriginFile.CreateNavigator()
Dim fileItr As XPathNodeIterator = fileNav.Select("//data")
Dim addToDestNodes As List(Of XmlNode) = New List(Of XmlNode)
While (fileItr.MoveNext())
Dim addNode As XmlNode = CType(fileItr.Current, IHasXmlNode).GetNode()
addToDestNodes.Add(addNode)
End While //loop thru nodes
If addToDestNodes.Count > 0 Then
For Each addedNode As XmlNode In addToDestNodes
Dim addTargetNode As XmlNode = xmlTargetFile.ImportNode(addedNode, True)
xmlTargetFile.DocumentElement.AppendChild(addTargetNode)
Next
End If
xmlTargetFile.Save(xmlTarget) //xmlTarget is a string that hold path to xml document
XML has a root element and you have to add new element within this root element.
XmlElement eleParent = docDestn.CreateElement("EleParent");
XmlElement eleChild = docDestn.CreateElement("Item");
eleParent.AppendChild(eleChild);
XMLNode rootNode= xmlDoc.SelectSingleNode("RootEle");
rootNode.AppendChild(eleParent);
Plps. refer the link for detail: http://navinpandit.blogspot.in/2016/12/exception-this-document-already-has.html

Resources