linq to xml 查询问题
首先我想问下XML我这样写可以么?
<root>
   <T name=t1>
      <D name=d1>ddd</D>
      <D name=d2>ccc</D>
   </T>
   <T name=t2>
   </T>
</root>
我想是这样查,先查根据name查到匹配的T,再在T中查到匹配name的D,更改D的value,如果T中不存在D的话,就新加一个D,并设置name和value
本来使用XPATH直接可以得到匹配name的T的,但是再用LINQ去查匹配的D的name我就不会了
或者,可以直接用linq来写完全过程么?
------解决方案--------------------XDocument doc = XDocument.Load("test.xml");
           var nodes = from h in doc.Descendants("T")
                       where h.Attribute("name").Value == "t1"
                       select h;
           if (nodes.Descendants("D").Count() > 0)
           {
               foreach (var item in nodes.Descendants("D"))
               {
                   item.Value = "要修改的值";
                   doc.Save("test.xml");
               }
           }
           else
           {
               XElement newEl = new XElement("D", new XAttribute("name", "要设定的name值"));
               nodes.First().Add(newEl);
               doc.Save("test.xml");
           }
试试
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------.net4.0没选错吧