我正在尝试将集合类转换为 xml 数据并将该 xml 数据附加到 xml 文件这是我的函数
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;
}
}
在上面的函数中,“femail”是一个List的参数,其中Email是用户定义的自定义类。第一次检查文件是否存在。如果不是,那么它现在可以正常工作,但是当我需要将数据附加到现有文件时,它会失败。
根据我上面的代码,它 <?xml version="1.0" encoding="utf-16"?>
使用我想在绑定之前删除的这个标签将单个电子邮件对象转换为 xml 数据。
我从我的代码中得到的输出是:
<?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 <rajendra.b000@gmail.com></From>
<To>Mansha Suman <asdmansha@gmail.com></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 <rajendra.b000@gmail.com></From>
<To>Mansha Suman <asdmansha@gmail.com></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>
这不是我所期望的。请建议我进行必要的更正。