Reputation: 361
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;
}
}
In the above function "femail" is parameter of a List, where Email is userdefind custom class. First time it checks if the file is existing or not. If not then it works fine now but when i need to append data to my existing file it fails.
As per my above code it converts single Email object to xml data with <?xml version="1.0" encoding="utf-16"?>
with this tags which i want to remove before bind.
The output i got from my code is:
<?xml version="1.0"?>
<ArrayOfEmail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Email>
<UtcDateTime>2013-01-23T10:09:48+05:30</UtcDateTime>
<From>Rajendra Bhalekar <[email protected]></From>
<To>Mansha Suman <[email protected]></To>
<Subject>Ye dil sun raha hai jo mere dil ki hai sada ye...socha tha na maine......</Subject>
<IDX>32a84ef7-31bf-4366-bcab-ec10b2f39bc6</IDX>
<Body>
Hi Raj,
Dil pe mat le yaar dil pe mat le....
idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6
odhani chunariya tere naam ki - Pyar kiya to darna kya
sona sona telephone dhun pe hasne wali - hidustaniREPLY ABOVE THIS LINEdil
ibadat kar raha hai
Thanks & regards,
Raj
</Body>
<ExtractReplyText>Hi Raj,Dil pe mat le yaar dil pe mat le....idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6odhani chunariya tere naam ki - Pyar kiya to darna kya</ExtractReplyText>
</Email>
<ArrayOfEmail>
<?xml version="1.0" encoding="utf-16"?>
<Email xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UtcDateTime>2013-01-23T10:09:48+05:30</UtcDateTime>
<From>Rajendra Bhalekar <[email protected]></From>
<To>Mansha Suman <[email protected]></To>
<Subject>Ye dil sun raha hai jo mere dil ki hai sada ye...socha tha na maine......</Subject>
<IDX>32a84ef7-31bf-4366-bcab-ec10b2f39bc6</IDX>
<Body>
Hi Raj,
Dil pe mat le yaar dil pe mat le....
idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6
odhani chunariya tere naam ki - Pyar kiya to darna kya
sona sona telephone dhun pe hasne wali - hidustaniREPLY ABOVE THIS LINEdil
ibadat kar raha hai
Thanks & regards,
Raj
</Body>
<ExtractReplyText>Hi Raj,Dil pe mat le yaar dil pe mat le....idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6odhani chunariya tere naam ki - Pyar kiya to darna kya</ExtractReplyText>
</Email></ArrayOfEmail>
</ArrayOfEmail>
Which is not as i am expecting. Please suggest me required corrections.
Upvotes: 2
Views: 5068
Reputation: 361
My problam is solve now and this code is working for me
public void XmlWriter(List<Email> Femails)
{
#region On Error Retry and retrycount with xml file
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);
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;
}
#endregion
}
Upvotes: 2