C# C如何提取完整的xml节点集
他们是否可以使用XPath选择完整的第一个节点集,例如C# C如何提取完整的xml节点集,c#,xpath,c#-3.0,C#,Xpath,C# 3.0,他们是否可以使用XPath选择完整的第一个节点集,例如 <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
因此,该xml块可以存储起来供以后使用
Bob.此查询将选择该节点。您是要获取一组节点还是仅获取一个节点?如果只需要节点的子集,则可能必须自己将书店节点放回原处
<book category="COOKING">
to
</book>,
作为XML文档
/bookstore/book[@category='COOKING']
作为XDocument
var x = new XmlDocument();
x.Load("XmlFile1.xml");
var ns = x.SelectSingleNode("/bookstore/book[@category='COOKING']");
var res = ns.OuterXml;
如果您只需要book节点,可以这样做,而不是上面的根版本
var x = XDocument.Load("XmlFile1.xml");
var root = new XElement("bookstore",
from book in x.Element("bookstore").Elements("book")
where book.Attribute("category").Value == "COOKING"
select book
);
马修的回答补充道:
var book = x.Element("bookstore")
.Elements("book")
.Where(n => n.Attribute("category").Value == "COOKING")
.First();
xNode现在相当于烹饪类书籍。假设此XML存储在名为doc的XML文档中 我对此进行了测试,并添加了这一行以验证:
XmlElement docRoot = doc.DocumentElement;
XmlNode cookingNode = docRoot.SelectSingleNode("./book[@category='COOKING']");
结果如下:
Console.WriteLine(cookingNode.OuterXml);
假设我只想提取xml文件如下所示的数据
<book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada
De Laurentiis</author><year>2005</year><price>30.00</price></book>
我的代码如下
lang auth
en up
我试过了。这只会返回烹饪节点。我想要的是把那个特定街区里的所有东西从带到。。。我正在阅读进入消息管道的xml消息。我需要存储它们的特定部分,以便将这些部分构建成一个新的复合消息,本质上是一个请求/回复。因此,我需要一种从传入消息中选择xml块的方法,以便在以后构建回复消息时使用。Xml.Linq类的工作方式相同。@作用域蠕变:从var ns=x.SelectSingleNode[…],您应该能够使用ns.ChildNodes访问该节点内的节点。您还可以检查.OuterXml,它将显示您所在的节点。如果你使用sheck.InnerXML,它只会显示孩子们。我想这会给你一个XmlNodeList而不是XmlNode。是的。。。您将需要获取第一个索引或使用SelectSingleNode。最终,我拥有满足我的需求的代码。。XmlDocument xDoc=新的XmlDocument;//将填充xDoc的代码放在这里XmlNodeList xNode=xDoc.SelectNodes@/bookstore/book[@category='COOKING']/title;
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author auth="up">Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
lang auth
en up
XmlNodeList elemList = doc.GetElementsByTagName("book");
for (int j = 0; j < elemList.Count; j++)
{
if (elemList[j].Attributes["category"].Value == "COOKING")
{
XmlNodeList elemList1 = doc.GetElementsByTagName("author");
for (int i = 0; i < elemList1.Count; i++)
{
string attrVal = elemList1[i].Attributes["lang"].Value;
string attrVal1 = elemList1[i].Attributes["auth"].Value;
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(attrVal1);
lvi.SubItems.Add(attrVal1);
}
listView1.Items.Add(lvi);
}
}
}