C# 使用LINQ交换元素
我有一个以下格式的xml文档:C# 使用LINQ交换元素,c#,xml,linq,C#,Xml,Linq,我有一个以下格式的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"> <spa
<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>
一个
这是span 1
这是span 2
我不太熟悉LINQ的使用,我想用它来交换上面代码的元素。(例如,我想搬家
<span>This is span 2</span>
这是span 2
进入
<child xml:id="1"></child>
元素树,反之亦然)
我正在运行的例子,但我真的很想在正确的方向上进一步推动(老实说,我很难找到从哪里开始!)
谢谢大家! 您可以使用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();
我相信这会满足你的要求:
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);
}
}
}
使用系统;
使用System.Xml;
使用System.Xml.Linq;
使用System.Xml.XPath;
名称空间示例
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=@“
一个
这是span 1
这是span 2
";
XDocument doc=XDocument.Parse(xml);
XmlNamespaceManager namespaceManager=新的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.替换为(span2);
span2.替换为(span1);
控制台写入线(doc);
}
}
}
您可以使用LINQ,但它实际上是用于查询,而不是修改。从技术上讲,您可以编写一个查询来提供所需的结果,但这将相当难看
我建议将字符串加载到XMLDocument中,并使用XPath交换节点:
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;
}
请注意,您需要使用XmlNamespaceManager来解释文档中的“xml”命名空间(我假设它是在其他地方定义的)。这个答案是否有帮助:?我想您希望LINQ to xml:这两个都非常有帮助。有一件事我不明白。。。XDocument与StreamReader类似吗?与中一样,XDocument对象是逐行读取XML输入,还是缓冲整个XML输入并将每个元素转换为对象,并允许直接操作?基本上,我要问的是,交换元素类似于交换变量,在这里你可以只交换x=temp,x=y,y=temp?@gfppaste XDocument将整个东西加载到内存中。我最终使用了这个解决方案和