<?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.
None
0 Points
5 Posts
How to append xml data into xml file
Feb 12, 2013 05:13 AM|rbhalek|LINK
I am trying to convert collection class to xml data and append that xml data to xml file here is my function
it append the new node with
All-Star
43204 Points
10243 Posts
MVP
Re: How to append xml data into xml file
Feb 12, 2013 05:25 AM|DarrellNorton|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:
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
None
0 Points
5 Posts
Re: How to append xml data into xml file
Feb 12, 2013 05:36 AM|rbhalek|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
All-Star
94130 Points
18109 Posts
Re: How to append xml data into xml file
Feb 13, 2013 03:29 AM|Decker Dong - MSFT|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?
None
0 Points
5 Posts
Re: How to append xml data into xml file
Feb 13, 2013 05:12 AM|rbhalek|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
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
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.
None
0 Points
5 Posts
Re: How to append xml data into xml file
Feb 14, 2013 01:50 AM|rbhalek|LINK
Thanks @Darrell Norton & Decker for your suggestions and help
My problam is solve now and here is the solution