使用 c# Xdocument 类添加子节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17916788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:48:22  来源:igfitidea点击:

Adding child nodes using c# Xdocument class

c#xml

提问by tjdoubts

I have an xml file as given below.

我有一个 xml 文件,如下所示。

<?xml version="1.0" encoding="utf-8"?>
 <file:Situattion xmlns:file="test">

  <file:Properties>

</file:Situattion>

I would like to add the child element file:Character using xDocument.So that my final xml would be like given below

我想添加子元素 file:Character using xDocument.So 这样我的最终 xml 将如下所示

<?xml version="1.0" encoding="utf-8"?>
  <file:Situattion xmlns:file="test">

   <file:Characters>

     <file:Character file:ID="File0">
     <file:Value>value0</file:Value>
     <file:Description>
      Description0 
     </file:Description>
     </file:Character>

 <file:Character file:ID="File1">
     <file:Value>value1</file:Value>
     <file:Description>
     Description1
     </file:Description>
     </file:Character>

     </file:Characters>

Code in c# i tried using Xdocument class is given below.

下面给出了我尝试使用 Xdocument 类的 c# 代码。

        XNamespace ns = "test";
        Document = XDocument.Load(Folderpath + "\File.test");

        if (Document.Descendants(ns + "Characters") != null)
        {

            Document.Add(new XElement(ns + "Character"));
        }
        Document.Save(Folderpath + "\File.test");

At line "Document.Add(new XElement(ns + "Character"));", I am getting an error:

在“ Document.Add(new XElement(ns + "Character"));”行,我收到一个错误:

"This operation would create an incorrectly structured document.".

"This operation would create an incorrectly structured document.".

How can I add the node under "file:Characters".

如何在“ file:Characters”下添加节点。

采纳答案by Jon Skeet

You're trying to add an extra file:Characterelement directlyinto the root. You don't want to do that - you want to add it under the file:Characterselement, presumably.

您正在尝试将额外的file:Character元素直接添加到根中。您不想这样做 - 您想将它添加到file:Characters元素下,大概。

Also note that Descendants()will neverreturn null - it will return an empty sequence if there are no matching elements. So you want:

另外请注意,Descendants()永远不会返回null -如果没有匹配的元素就会返回一个空序列。所以你要:

var ns = "test";
var file = Path.Combine(folderPath, "File.test");
var doc = XDocument.Load(file);
// Or var characters = document.Root.Element(ns + "Characters")
var characters = document.Descendants(ns + "Characters").FirstOrDefault();
if (characters != null)
{
    characters.Add(new XElement(ns + "Character");
    doc.Save(file);
}

Note that I've used more conventional naming, Path.Combine, and also moved the Savecall so that you'll only end up saving if you've actually made a change to the document.

请注意,我使用了更传统的命名方式 ,Path.Combine并且还移动了Save调用,因此只有在您实际对文档进行了更改时才能最终保存。

回答by Prash

    Document.Root.Element("Characters").Add(new XElement("Character", new XAttribute("ID", "File0"), new XElement("Value", "value0"), new XElement("Description")),
        new XElement("Character", new XAttribute("ID", "File1"), new XElement("Value", "value1"), new XElement("Description")));

Note:I have not included the namespace for brevity. You have to add those.

注意:为简洁起见,我没有包含命名空间。你必须添加这些。