Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML节点附加到现有XML文件_C#_.net_Linq To Xml - Fatal编程技术网

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

C# 将XML节点附加到现有XML文件,c#,.net,linq-to-xml,C#,.net,Linq To Xml,我有一个以下格式的XML <Attachment> <AttachmentName>Top Nav Menu.docx</AttachmentName> <Subject>Attachment1</Subject> <Sender>JameelM@orioninc.com</Sender> </Attachment> 为附件创建根节点: var doc = new XDocument(

我有一个以下格式的XML

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

为附件创建根节点:

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

为附件创建根节点:

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

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


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

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


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

附件是XML的根节点。是否要有多个根元素?没有根元素。我需要在xml的
XDocument
中添加一个新的附件,就像我在结束附件nodeWell LINQ之后共享的xml格式一样,该附件的目的是对xml规范定义为格式良好的文档进行操作。良好格式要求之一是包含所有其他元素的单个根元素。因此,如果您想要有两个顶级的
附件
元素节点,那么您需要构造一个非XML文档且不能用
XDocument
表示的内容。您确定要这样做吗?是否需要为此添加根元素?如果要有重复元素,则父节点(例如附件)是一个非常好的主意,因为您显然想重复根元素,即使它起作用,它也不再是有效的xml。附件是xml的根节点。是否要有多个根元素?没有根元素。我需要在xml的
XDocument
中添加一个新的附件,就像我在结束附件nodeWell LINQ之后共享的xml格式一样,该附件的目的是对xml规范定义为格式良好的文档进行操作。良好格式要求之一是包含所有其他元素的单个根元素。因此,如果您想要有两个顶级的
附件
元素节点,那么您需要构造一个非XML文档且不能用
XDocument
表示的内容。您确定要这样做吗?是否需要为此添加根元素?如果要有重复元素,父节点(例如附件)是一个非常好的主意,因为您显然想要重复根元素,即使它起作用,它也不再是有效的xml。