Ric*_*ich 140 .net c# xml linq linq-to-xml
我有一个XDocument对象.我想使用LINQ在任何深度查询具有特定名称的元素.当我使用时Descendants("element_name"),我只获得了当前级别的直接子元素.我正在寻找的是相当于XPath中的"// element_name"...我应该使用XPath,还是有办法使用LINQ方法?谢谢.
Jon*_*eet 208
后代应该绝对正常.这是一个例子:
using System;
using System.Xml.Linq;
class Test
{
static void Main()
{
string xml = @"
<root>
<child id='1'/>
<child id='2'>
<grandchild id='3' />
<grandchild id='4' />
</child>
</root>";
XDocument doc = XDocument.Parse(xml);
foreach (XElement element in doc.Descendants("grandchild"))
{
Console.WriteLine(element);
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
<grandchild id="3" />
<grandchild id="4" />
Jel*_*gab 54
表示命名空间的示例:
String TheDocumentContent =
@"
<TheNamespace:root xmlns:TheNamespace = 'http://www.w3.org/2001/XMLSchema' >
<TheNamespace:GrandParent>
<TheNamespace:Parent>
<TheNamespace:Child theName = 'Fred' />
<TheNamespace:Child theName = 'Gabi' />
<TheNamespace:Child theName = 'George'/>
<TheNamespace:Child theName = 'Grace' />
<TheNamespace:Child theName = 'Sam' />
</TheNamespace:Parent>
</TheNamespace:GrandParent>
</TheNamespace:root>
";
XDocument TheDocument = XDocument.Parse( TheDocumentContent );
//Example 1:
var TheElements1 =
from
AnyElement
in
TheDocument.Descendants( "{http://www.w3.org/2001/XMLSchema}Child" )
select
AnyElement;
ResultsTxt.AppendText( TheElements1.Count().ToString() );
//Example 2:
var TheElements2 =
from
AnyElement
in
TheDocument.Descendants( "{http://www.w3.org/2001/XMLSchema}Child" )
where
AnyElement.Attribute( "theName" ).Value.StartsWith( "G" )
select
AnyElement;
foreach ( XElement CurrentElement in TheElements2 )
{
ResultsTxt.AppendText( "\r\n" + CurrentElement.Attribute( "theName" ).Value );
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*ein 36
你可以这样做:
xml.Descendants().Where(p => p.Name.LocalName == "Name of the node to find")
Run Code Online (Sandbox Code Playgroud)
这里xml是一个XDocument.
请注意,该属性Name返回一个具有a LocalName和a 的对象Namespace.这就是为什么你要使用,Name.LocalName如果你想按名称进行比较.
小智 11
有两种方法可以实现这一目标,
以下是使用这些方法的示例,
List<XElement> result = doc.Root.Element("emails").Elements("emailAddress").ToList();
Run Code Online (Sandbox Code Playgroud)
如果使用XPath,则需要对IEnumerable进行一些操作:
IEnumerable<XElement> mails = ((IEnumerable)doc.XPathEvaluate("/emails/emailAddress")).Cast<XElement>();
Run Code Online (Sandbox Code Playgroud)
注意
var res = doc.XPathEvaluate("/emails/emailAddress");
Run Code Online (Sandbox Code Playgroud)
结果是空指针,或没有结果.
我使用的XPathSelectElements扩展方法与方法的工作方式相同XmlDocument.SelectNodes:
using System;
using System.Xml.Linq;
using System.Xml.XPath; // for XPathSelectElements
namespace testconsoleApp
{
class Program
{
static void Main(string[] args)
{
XDocument xdoc = XDocument.Parse(
@"<root>
<child>
<name>john</name>
</child>
<child>
<name>fred</name>
</child>
<child>
<name>mark</name>
</child>
</root>");
foreach (var childElem in xdoc.XPathSelectElements("//child"))
{
string childName = childElem.Element("name").Value;
Console.WriteLine(childName);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)