c#linq更改包含xml标记的xml元素的值

时间:2015-08-11 18:22:49

标签: c# xml linq xelement setvalue

我使用以下源xml文件

进行了测试
    <?xml version="1.0" encoding="utf-8" standalone="no"?>
<root>
    <key name="some text" id="1"/>
    <key name="some text" id="2"/>
    <house id="H1">
        <floor id="F1">
            <room id="R1">Child1<electrics><socket id="C1S1"/></electrics></room>
            <room id="R2">Child2<electrics><socket id="C2S1"/></electrics></room>
        </floor>
    </house>
</root>

我想复制/克隆完整的标签(房间ID =&#34; R1&#34;)并粘贴到最后一个房间内。同时我想将(room id =&#34; R1&#34;)的标签值更改为&#34; Whatever&#34;和(房间)内的(套接字ID)-Tag到&#34;新房间&#34;

最后我想得到以下xml-structure

    <?xml version="1.0" encoding="utf-8"?>
<root>
  <key name="some text" id="1" />
  <key name="some text" id="2" />
  <house id="H1">
    <floor id="F1">
      <room id="R1">Child1<electrics><socket id="C1S1" /></electrics></room>
      <room id="R2">Child2<electrics><socket id="C2S1" /></electrics></room>
      <room id="R1">Whatever<electrics><socket id="new room" /></electrics></room>
    </floor>
  </house>
</root>

但我有问题。创建克隆后,我无法更改新房间的值,而不删除嵌套标签。 第三个最后一个命令导致了我的问题。如果你跳过那个命令,你会得到一个近乎完美的克隆,但我不知道如何改变它。

这是我的c#代码

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml.Linq;

namespace linqxmlTester
{
    public partial class Form1 : Form
    {
        private XElement xmlDoc;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {

            xmlDoc = XElement.Load("house.xml");

            // Select rooms to clone I realy only want to selekt one room, but I have to go over rooms
            IEnumerable<XElement> newRoom = from rooms in xmlDoc.Element ("house")
                                                                .Element ("floor")
                                                                .Elements("room")

                                                    where rooms.ToString().IndexOf("C1S1") > 0

                                                    select rooms;
            // I've selected realy one room in rooms with the following I clone beheind last room
            xmlDoc.Element ("house")
                  .Element ("floor")
                  .Elements("room").Last()
                  .AddAfterSelf(newRoom.First()); // there is only one

            // This worked   :-)

            //But now I want to change socket id value (="C1S1") to something new
            //I select my last created room

            XElement newRoom1 = xmlDoc.Element("house")
                                      .Element("floor")
                                      .Elements("room").Last();

            //within the newRoom1 I search for the attribute id

            foreach (var r in newRoom1.Elements("electrics").Elements("socket"))
            {
                Debug.WriteLine("aktValue " + r.Attribute("id").Value);
                r.Attribute("id").Value = "new room";
                Debug.WriteLine("newValue " + r.Attribute("id").Value);
            }

            // this is working too :-)

            // Now I only still have to change the value of my newroom
            // it should be the following code

            Debug.WriteLine("OldValue " + newRoom1.Value); // prints only the value without the embeded xml tags

            newRoom1.SetValue("Whatever"); // this kills the complete xml structure with is embede at the side of the value

            Debug.WriteLine("NewValue " + newRoom1.Value); // prints only the newvalue without the embeded xml tags

            xmlDoc.Save("housenew.xml");
        }
}
}

如何在不删除嵌入式xml结构的情况下使用linq更改c#中的值?

感谢您的提示

2 个答案:

答案 0 :(得分:1)

您必须将room元素视为树的节点。下面的所有内容也是一个节点,因此事实上,Child1是节点(文本),electrics是节点(元素)。

视觉表示:

* Element: room (with attribute id)
|- Text: Child1
|- Element: electrics
   |- Element: socket (with attribute id)

因此,要访问该文本节点(Child1),只需更改room的第一个子节点,如下所示:

newRoom1.Nodes().First().ReplaceWith("Whatever");

如果您更改了.Value的空间,则需要替换room的整个子树。

但是,如果可能的话,我会避免使用这种结构。更改&#39; Child1&#39; value,以便它是房间的属性(首选),或将其包装在节点中,以便<room id="1"><description>Child1</description></room>

答案 1 :(得分:0)

您选择那条行

 class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>" +
                "<root>" +
                    "<key name=\"some text\" id=\"1\"/>" +
                    "<key name=\"some text\" id=\"2\"/>" +
                    "<house id=\"H1\">" +
                        "<floor id=\"F1\">" +
                            "<room id=\"R1\">Child1<electrics><socket id=\"C1S1\"/></electrics></room>" +
                            "<room id=\"R2\">Child2<electrics><socket id=\"C2S1\"/></electrics></room>" +
                        "</floor>" +
                    "</house>" +
                "</root>";

            XElement xmlDoc = XElement.Parse(input);
            XElement floor = xmlDoc.Descendants("floor").FirstOrDefault();
            XElement newR1 = new XElement(floor.Elements("room").Where(x => x.Attribute("id").Value == "R1").FirstOrDefault());
            XAttribute socket = newR1.Element("electrics").Element("socket").Attribute("id");
            socket.Value = "new room";
            floor.Add(newR1);
        }
    }
}

这是XML结果

<root>
  <key name="some text" id="1" />
  <key name="some text" id="2" />
  <house id="H1">
    <floor id="F1">
      <room id="R1">Child1<electrics><socket id="C1S1" /></electrics></room>
      <room id="R2">Child2<electrics><socket id="C2S1" /></electrics></room>
      <room id="R1">Child1<electrics><socket id="new room" /></electrics></room>
    </floor>
  </house>
</root>