【问题标题】:C# XElement - add element before value of existing elementC# XElement - 在现有元素的值之前添加元素
【发布时间】:2016-10-28 09:35:26
【问题描述】:

我有一个这样的 XML 字符串:

<para>Some text in here</para>

我需要在开始的 para 标记之后添加另一个元素,以便它读取

<para><title>My Title</title>Some text in here</para>

我试过了,但它没有给我我需要的东西:

content.Descendants("para") 
               .LastOrDefault()
               .Add(new XElement("title", "My Title"));

问题在于它将&lt;title&gt; 元素及其内容添加到关闭&lt;para&gt; 标记之前。

请问我怎样才能把它拿到我想要的地方?

【问题讨论】:

    标签: c# xml linq-to-xml xelement


    【解决方案1】:

    试试这个

    string xml = "<para>Some text in here</para>";
                XElement para = XElement.Parse(xml);
    
                para.AddFirst(new XElement("title", "My Title"));
    

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多