2012-06-07 33 views
2

交換的元素,我有以下格式的XML文檔:與LINQ

<body> 
    <par id = "1"> 
     <prop type="Content">One</prop> 
     <child xml:id="1"> 
     <span>This is span 1</span> 
     </child> 
     <child xml:id="2"> 
     <span>This is span 2</span> 
     </child> 
    </par> 
</body> 

我不是很熟悉使用LINQ,我想用它來交換上面的代碼中的元素。 (IE,我想移動

<span>This is span 2</span> 

<child xml:id="1"></child> 

元素樹,和正相反)

我通過實例運行在http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b,但我真的很喜歡在正確的方向上進一步推動(我真的很難找出從哪裏開始!)。

謝謝!

+0

此答案幫助:http://stackoverflow.com/questions/3740627/how-to-swap-two-xml-elements-in-linq-to-xml? –

+0

我認爲你需要LINQ to XML:http://msdn.microsoft.com/en-us/library/bb387098.aspx – devuxer

+0

這兩個幫助很大。有一件事我不理解......是類似於StreamReader的XDocument?如在中,XDocument對象會逐行讀取XML輸入,還是會緩衝整個XML輸入並將每個元素轉換爲對象,並允許直接操作?基本上,我問的是,交換XElements類似於交換變量,在那裏你可以只是爲了x = temp,x = y,y = temp? – gfppaste

回答

1

您可以使用LINQ到SML查詢

var retmodule = (from c in xdoc.Elements("body").Elements("par").Elements("child"). 
          where c.Attribute("xml:id").Value == 2 
          select new 
          { 
           c.Element("span").Value 
          }).SingleOrDefault(); 

,然後使用XPath和XML庫的組合

XElement parentXElement = xmldoc.XPathSelectElement("products"); 
XElement refXElement = xdoc.XPathSelectElement("body/par/child [@xml:id = '2']"); 
XElement newXElement = new XElement("span", retmodule.ToString()); 
refXElement.AddAfterSelf(newXElement); 
xdoc.Save(); 
+0

我結束了使用此解決方案的組合和http://stackoverflow.com/questions/10655068/xelement-linq-code-failure – gfppaste

1

我相信這將做你想做的插入選擇特定elemnt :

using System; 
using System.Xml; 
using System.Xml.Linq; 
using System.Xml.XPath; 

namespace Example 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xml = @" 
<body> 
    <par id = ""1""> 
     <prop type=""Content"">One</prop> 
     <child xml:id=""1""> 
     <span>This is span 1</span> 
     </child> 
     <child xml:id=""2""> 
     <span>This is span 2</span> 
     </child> 
    </par> 
</body>"; 
      XDocument doc = XDocument.Parse(xml); 
      XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.CreateNavigator().NameTable); 
      namespaceManager.AddNamespace("xml", "http://www.w3.org/XML/1998/namespace"); 
      XElement span1 = doc.Root.XPathSelectElement(@"/body/par/child[@xml:id = ""1""]/span[1]", namespaceManager); 
      XElement span2 = doc.Root.XPathSelectElement(@"/body/par/child[@xml:id = ""2""]/span[1]", namespaceManager); 

      span1.ReplaceWith(span2); 
      span2.ReplaceWith(span1); 

      Console.WriteLine(doc); 
     } 
    } 
} 
1

可以使用LINQ,但它的[R eally意味着查詢,而不是修改。從技術上講,你可以寫一個查詢來給你想要的結果,但這會相當難看。

我會建議使用XPath加載串入一個XMLDocument和換出節點:

private static string swap(string xml) 
    { 
     XmlDocument xDoc = new XmlDocument(); 
     xDoc.LoadXml(xml); 

     XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xDoc.NameTable); 
     namespaceManager.AddNamespace("xml", "http://www.w3.org/XML/1998/namespace"); 

     var node1 = xDoc.SelectSingleNode(@"//child[@xml:id=""1""]",namespaceManager); 
     var node2 = xDoc.SelectSingleNode(@"//child[@xml:id=""2""]",namespaceManager); 

     var temp = node1.InnerXml; 
     node1.InnerXml = node2.InnerXml; 
     node2.InnerXml = temp; 

     return xDoc.OuterXml; 
    } 

請注意,你需要考慮你的文檔中的「XML」命名空間(我假設是定義在其他地方)通過使用XmlNamespaceManager。