将XML节点附加到现有XML文件

时间:2021-01-14 14:05:38

I have an XML in the following format

我有以下格式的XML

<Attachment>
 <AttachmentName>Top Nav Menu.docx</AttachmentName>
 <Subject>Attachment1</Subject>
 <Sender>JameelM@orioninc.com</Sender>
 </Attachment>

I want to attach another Attachment like above after the attachment close node. Below are the code that i have written for writing xml file

我想在附件关闭节点之后附加上面的另一个附件。下面是我为编写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");

2 个解决方案

#1


3  

Create root node for your attachments:

为附件创建根节点:

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

When you decide to append another attachment, load document and add attachment to root:

当您决定附加其他附件时,请加载文档并向root添加附件:

doc.Root.Add(new XElement("Attachment",
    new XElement("AttachmentName", attachment.Name),
    new XElement("Subject", exchangeEmailInformation.Subject),
    new XElement("Sender", exchangeEmailInformation.Sender)
));

#2


0  

I would use the XMLSerializer class. There you can handle your XML files just as if they are classes. Just have a look, you will like it :)

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

Load XML -> Use Classes in Code (modify, delete, add) -> Serialize back into XML

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

#1


3  

Create root node for your attachments:

为附件创建根节点:

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

When you decide to append another attachment, load document and add attachment to root:

当您决定附加其他附件时,请加载文档并向root添加附件:

doc.Root.Add(new XElement("Attachment",
    new XElement("AttachmentName", attachment.Name),
    new XElement("Subject", exchangeEmailInformation.Subject),
    new XElement("Sender", exchangeEmailInformation.Sender)
));

#2


0  

I would use the XMLSerializer class. There you can handle your XML files just as if they are classes. Just have a look, you will like it :)

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

Load XML -> Use Classes in Code (modify, delete, add) -> Serialize back into XML

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