0

我有以下格式的 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");
4

2 回答 2

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)
    )));

当您决定附加另一个附件时,加载文档并将附件添加到根目录:

doc.Root.Add(new XElement("Attachment",
    new XElement("AttachmentName", attachment.Name),
    new XElement("Subject", exchangeEmailInformation.Subject),
    new XElement("Sender", exchangeEmailInformation.Sender)
));
于 2012-12-24T14:44:25.043 回答
0

我会使用 XMLSerializer 类。在那里,您可以像处理类一样处理您的 XML 文件。看看吧,你会喜欢的:)

加载 XML -> 在代码中使用类(修改、删除、添加)-> 序列化回 XML

于 2012-12-24T22:29:15.860 回答