我有以下格式的XML
<Attachment>
<AttachmentName>Top Nav Menu.docx</AttachmentName>
<Subject>Attachment1</Subject>
<Sender>JameelM@orioninc.com</Sender>
</Attachment>
我想在附件关闭节点之后附加上面的另一个附件。下面是我为编写xml文件而编写的代码
var doc = new XDocument(
new XDeclaration("1.0", "utf-16", "true"),
new XProcessingInstruction("test", "value"),
new XElement("Attachment",new XElement("AttachmentName", attachment.Name),
new XElement("Subject", exchangeEmailInformation.Subject),
new XElement("Sender", exchangeEmailInformation.Sender
)));
doc.Save(ConfigInformation.BackUpPath + FolderId[index]+"\\Attachments"+index+".xml");
答案 0 :(得分:3)
为附件创建根节点:
var doc = new XDocument(
new XDeclaration("1.0", "utf-16", "true"),
new XProcessingInstruction("test", "value"),
new XElement("Attachments",
new XElement("Attachment",
new XElement("AttachmentName", attachment.Name),
new XElement("Subject", exchangeEmailInformation.Subject),
new XElement("Sender", exchangeEmailInformation.Sender)
)));
当您决定附加其他附件时,请加载文档并向root添加附件:
doc.Root.Add(new XElement("Attachment",
new XElement("AttachmentName", attachment.Name),
new XElement("Subject", exchangeEmailInformation.Subject),
new XElement("Sender", exchangeEmailInformation.Sender)
));
答案 1 :(得分:0)
我会使用XMLSerializer类。在那里,您可以像处理类一样处理XML文件。看看,你会喜欢它:))
加载XML - &gt;使用代码中的类(修改,删除,添加) - &gt;序列化回XML