<?xml version="1.0"?> xml infomation also for example
<?xml version="1.0" encoding="utf-16"?><parentnode><node> </node></parentndoe> its my orginal xml doc ok. when i bind new node to my xml doc its bind as per given below <?xml version="1.0" encoding="utf-16"?> <parentnode><node></node> <?xml version="1.0" encoding="utf-16"?><parentnode><node></node></parentnode></parentnode>
both the code give me same answer because when it create xml data from object then it already have the version info of xml and when code add node to xml doc it fails with vaidate document for example
<?xml version="1.0" encoding="utf-16"?><parentnode><node> </node></parentndoe> its my orginal xml doc ok. when i bind new node to my xml doc its bind as per given below <?xml version="1.0" encoding="utf-16"?> <parentnode><node></node> <?xml version="1.0" encoding="utf-16"?><parentnode><node></node></parentnode></parentnode>
but yes definatly your suggestion is 100% correct that there is no neccesity to load and save document at every interation
Actully i want write a xml file of my custom colleciton list object when first time i create a file from my object uing xml serialization it works fine but when the file is present in my target location i want to append my object data as xml to existing
file here that code
public void XmlWriter(List<Email> Femails) //function with parameter list object
{
string ErrorXmlFile = @"../../Retries.xml"; //target file location
try
{
if (!File.Exists(ErrorXmlFile)) //check file exist or not
{
//XmlSerializer with my custom class type Email
XmlSerializer xSeriz = new XmlSerializer(typeof(List<Email>));
FileStream fs = File.Open(ErrorXmlFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
//Fill filestream with xml data
xSeriz.Serialize(fs, Femails);
}
else //if file exist ie already have xml data
{
foreach(Email email in Femails) //serializing each object to xml
{
XmlDocument doc = new XmlDocument();
doc.Load(ErrorXmlFile); // Load xml file data to xmlDocument
//Create one xmlNode of xmldocument
XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null);
XmlSerializer xSeriz = new XmlSerializer(typeof(Email));
//stringwriter declaration
StringWriter sw = new StringWriter();
//fill stringwriter with xml data
xSeriz.Serialize(sw,email);
assign xml data to xnode
xnode.InnerXml = Convert.ToString(sw);
//Append xnode to my xmldocument
doc.DocumentElement.AppendChild(xnode);
//Save doc as file again
doc.Save(ErrorXmlFile);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Now what happen when i run my code first time it create xml file perfect. But when file exist it not working proprerly
for exmple: File exist with this data:
<?xml version="1.0"?>
<ArrayOfEmail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Email>
</Email>
<Email>
</Email>
<Email>
</Email>
</ArrayOfEmail>
Now when it append my new node to this document it append like this
<?xml version="1.0"?>
<ArrayOfEmail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Email>
</Email>
<Email>
</Email>
<Email>
</Email>
<?xml version="1.0"?>
<ArrayOfEmail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Email>
</Email>
</ArrayOfEmail>
</ArrayOfEmail>
when it appen new node to document it append xml version tag and namespace attribute also which I dont want to append As per MVP Suggestion
XmlDocument doc = new XmlDocument();
doc.Load(ErrorXmlFile);
foreach(Email email in Femails)
{
XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null);
XmlSerializer xSeriz = new XmlSerializer(typeof(Email));
StringWriter sw = new StringWriter();
xSeriz.Serialize(sw,email);
xnode.InnerXml = Convert.ToString(sw);
doc.DocumentElement.AppendChild(xnode);
}
doc.Save(ErrorXmlFile);
If i move
XmlDocument doc = new XmlDocument();
doc.Load(ErrorXmlFile);
and
doc.Save(ErrorXmlFile);
code to out of loop its giving same result like my code just performancewise its good because it load and save
Xml doc only once while my code load and save in every iteration. So its good suggestion but not solving my issue.
Any suggestion are greatly Appreciated
Thanks.
rbhalek
0 Points
5 Posts
How to append xml data into xml file
Feb 12, 2013 09:13 AM|LINK
I am trying to convert collection class to xml data and append that xml data to xml file here is my function
public void XmlWriter(List<Email> Femails) { string ErrorXmlFile = @"../../Retries.xml"; try { if (!File.Exists(ErrorXmlFile)) { XmlSerializer xSeriz = new XmlSerializer(typeof(List<Email>)); FileStream fs = File.Open(ErrorXmlFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); xSeriz.Serialize(fs, Femails); } else { foreach(Email email in Femails) { XmlDocument doc = new XmlDocument(); doc.Load(ErrorXmlFile); XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null); XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); StringWriter sw = new StringWriter(); xSeriz.Serialize(sw,email); xnode.InnerXml = Convert.ToString(sw); doc.DocumentElement.AppendChild(xnode); doc.Save(ErrorXmlFile); } } } catch (Exception ex) { throw ex; } }it append the new node with
DarrellNorto...
All-Star
86665 Points
9634 Posts
Moderator
MVP
Re: How to append xml data into xml file
Feb 12, 2013 09:25 AM|LINK
It's doing that because you are creating an XmlDocument for every loop iteration. Move the XmlDocument related code out of the loop, like this:
XmlDocument doc = new XmlDocument(); doc.Load(ErrorXmlFile); foreach(Email email in Femails) { XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null); XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); StringWriter sw = new StringWriter(); xSeriz.Serialize(sw,email); xnode.InnerXml = Convert.ToString(sw); doc.DocumentElement.AppendChild(xnode); } doc.Save(ErrorXmlFile);Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
rbhalek
0 Points
5 Posts
Re: How to append xml data into xml file
Feb 12, 2013 09:36 AM|LINK
both the code give me same answer because when it create xml data from object then it already have the version info of xml and when code add node to xml doc it fails with vaidate document for example
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to append xml data into xml file
Feb 13, 2013 07:29 AM|LINK
Hi rbhalek,
I think the MVP's reply is quite good——
Can you elebrate:
1) What's the actual your problem is (because your codes and your description is at a mess).
2) What's the expected result?
rbhalek
0 Points
5 Posts
Re: How to append xml data into xml file
Feb 13, 2013 09:12 AM|LINK
Thanks for your reply.
Actully i want write a xml file of my custom colleciton list object when first time i create a file from my object uing xml serialization it works fine but when the file is present in my target location i want to append my object data as xml to existing file here that code
public void XmlWriter(List<Email> Femails) //function with parameter list object { string ErrorXmlFile = @"../../Retries.xml"; //target file location try { if (!File.Exists(ErrorXmlFile)) //check file exist or not { //XmlSerializer with my custom class type Email XmlSerializer xSeriz = new XmlSerializer(typeof(List<Email>)); FileStream fs = File.Open(ErrorXmlFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); //Fill filestream with xml data xSeriz.Serialize(fs, Femails); } else //if file exist ie already have xml data { foreach(Email email in Femails) //serializing each object to xml { XmlDocument doc = new XmlDocument(); doc.Load(ErrorXmlFile); // Load xml file data to xmlDocument //Create one xmlNode of xmldocument XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null); XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); //stringwriter declaration StringWriter sw = new StringWriter(); //fill stringwriter with xml data xSeriz.Serialize(sw,email); assign xml data to xnode xnode.InnerXml = Convert.ToString(sw); //Append xnode to my xmldocument doc.DocumentElement.AppendChild(xnode); //Save doc as file again doc.Save(ErrorXmlFile); } } } catch (Exception ex) { throw ex; } }Now what happen when i run my code first time it create xml file perfect. But when file exist it not working proprerly
for exmple: File exist with this data:
when it appen new node to document it append xml version tag and namespace attribute also which I dont want to append
As per MVP Suggestion
XmlDocument doc = new XmlDocument(); doc.Load(ErrorXmlFile); foreach(Email email in Femails) { XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null); XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); StringWriter sw = new StringWriter(); xSeriz.Serialize(sw,email); xnode.InnerXml = Convert.ToString(sw); doc.DocumentElement.AppendChild(xnode); } doc.Save(ErrorXmlFile);If i move
XmlDocument doc = new XmlDocument();
doc.Load(ErrorXmlFile);
and
doc.Save(ErrorXmlFile);
code to out of loop its giving same result like my code just performancewise its good because it load and save
Xml doc only once while my code load and save in every iteration. So its good suggestion but not solving my issue.
Any suggestion are greatly Appreciated
Thanks.
rbhalek
0 Points
5 Posts
Re: How to append xml data into xml file
Feb 14, 2013 05:50 AM|LINK
Thanks @Darrell Norton & Decker for your suggestions and help
My problam is solve now and here is the solution
try { if (!File.Exists(ErrorXmlFile)) { XmlSerializer xSeriz = new XmlSerializer(typeof(List<Email>)); FileStream fs = File.Open(ErrorXmlFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); xSeriz.Serialize(fs, Femails); foreach (Email email in Femails) { SaveAttachment(email); } } else { XmlDocument doc = new XmlDocument(); doc.Load(ErrorXmlFile); foreach (Email email in Femails) { XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "BLACKswastik", null); XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlWriterSettings writtersetting = new XmlWriterSettings(); writtersetting.OmitXmlDeclaration = true; StringWriter stringwriter = new StringWriter(); using (XmlWriter xmlwriter = System.Xml.XmlWriter.Create(stringwriter, writtersetting)) { xSeriz.Serialize(xmlwriter, email, ns); } xnode.InnerXml = stringwriter.ToString(); XmlNode bindxnode = xnode.SelectSingleNode("Email"); doc.DocumentElement.AppendChild(bindxnode); SaveAttachment(email); } doc.Save(ErrorXmlFile); } } catch (Exception ex) { throw ex; }