如何将xml数据附加到xml文件中
我试图将集合类转换为xml数据并将xml数据附加到xml文件这里是我的函数’
public void XmlWriter(List Femails) { string ErrorXmlFile = @"../../Retries.xml"; try { if (!File.Exists(ErrorXmlFile)) { XmlSerializer xSeriz = new XmlSerializer(typeof(List)); 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是userdefind自定义类。 第一次检查文件是否存在。 如果没有那么它现在工作正常,但是当我需要将数据附加到我现有的文件时它失败了。
根据我上面的代码,它使用将单个Email对象转换为xml数据,并在绑定之前删除此标记。
我从我的代码得到的输出是:
2013-01-23T10:09:48+05:30 Rajendra Bhalekar <rajendra.b000@gmail.com> Mansha Suman <asdmansha@gmail.com> Ye dil sun raha hai jo mere dil ki hai sada ye...socha tha na maine...... 32a84ef7-31bf-4366-bcab-ec10b2f39bc6 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 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 2013-01-23T10:09:48+05:30 Rajendra Bhalekar <rajendra.b000@gmail.com> Mansha Suman <asdmansha@gmail.com> Ye dil sun raha hai jo mere dil ki hai sada ye...socha tha na maine...... 32a84ef7-31bf-4366-bcab-ec10b2f39bc6 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 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
这不是我所期待的。 请建议我需要更正。
我的问题现在解决了,这段代码对我有用
public void XmlWriter(List 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 )); 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 }
- C#中的标准正态分布z值函数
- Visual Studio:调试时重置用户设置
- 为C#接口定义隐式和显式转换
- TransactionScope助手无故耗尽连接池 – 帮助?
- Silverlight Assembly.Load()仅适用于完整/强名称
- 哪个更快:Automapper,Valuinjector还是手动映射? 每个人的速度到什么程度?
- 从前端javascript中加载的DLL中调用函数(在clientside javascript中加载dll)
- 更改Winforms菜单下拉列表的边框颜色
- 如何将凭据传递给计算机,以便我可以使用Microsoft.Win32.RegistryKey.OpenRemoteBaseKey()?