从XNode中读取值。

时间:2022-09-19 20:51:50

I have some code that is returning a XNode to me which looks like this:

我有一些代码返回一个XNode,它看起来是这样的:

<File>
  <Component>Main</Component>
  <Path>C:\Main\</Path>
  <FileName>main.txt</FileName>
</File>

I need to have some C# code that will be able to pull out the value of Path for example (C:\Main). I know that if I was using an XML node I could do it like this:

我需要一些c#代码,以便能够提取出Path的值(C:\Main)。我知道如果我使用一个XML节点,我可以这样做:

String filePath = xmlNode["Path"].InnerText;

Does anybody know what the equivalent would be for an XNode? Any help is much appreciated!

有人知道XNode的等效值是多少吗?非常感谢您的帮助!

5 个解决方案

#1


43  

Do you have to have it returning an XNode rather than an XElement? With an XElement it's simpler than with an XNode:

您是否必须让它返回一个XNode而不是XElement?使用XElement,它比使用XNode更简单:

string filePath = fileElement.Element("Path").Value;

That will find the first Path element, and will throw a NullReferenceException if there aren't any. An alternative if you're happy to get null if there aren't any would be:

这将找到第一个路径元素,如果没有,将抛出NullReferenceException。另一种方法是,如果你很高兴,如果没有的话,你将会得到null:

string filePath = (string) fileElement.Element("Path");

If you're really stuck with XNode, you'll either have to cast to XElement or possibly XContainer.

如果您真的被XNode卡住了,那么您将不得不转换到XElement或XContainer。

#2


7  

You can convert your XNode into XElement to access to its properties, my example:

您可以将XNode转换为XElement来访问它的属性,我的示例:

XNode lastNode = myXElement.LastNode;

//if I want to get the 'ID' attribute
string id = (lastNode as XElement).Attribute("ID").Value;

#3


0  

You may use this:

你可以用这个:

XElement xtr = XElement.Load("path/to/your/xml/file");
String filePath = xtr.Descendants("Path").Single().Value;

#4


0  

If you import System.Xml.XPath you can use XPathSelectElement like this on the XNode object:

如果你进口System.Xml。XPath可以在XNode对象上使用XPathSelectElement:

String component = xmlNode.XPathSelectElement("Component");
String path = xmlNode.XPathSelectElement("Path");
String fileName = xmlNode.XPathSelectElement("FileName");

#5


0  

Casting XNode to XElement works for the individual element to retrieve its value or attributes. But you won't be able to use myXelement.Elements("XXX") to get nested elements. For that you can use xmlNode.Nodes().

将XNode转换为XElement为单个元素工作,以检索其值或属性。但是,您将无法使用myXelement.Elements(“XXX”)来获取嵌套元素。为此,可以使用xmlNode.Nodes()。

This should work:

这应该工作:

var nodes = xmlNode.Nodes();//Get all nodes under 'File'
var fileNameNode = nodes.Where(el => ((XElement)el).Name.LocalName == "FileName")
.FirstOrDefault();
string filePath = ((XElement)fileNameNode).Value;

#1


43  

Do you have to have it returning an XNode rather than an XElement? With an XElement it's simpler than with an XNode:

您是否必须让它返回一个XNode而不是XElement?使用XElement,它比使用XNode更简单:

string filePath = fileElement.Element("Path").Value;

That will find the first Path element, and will throw a NullReferenceException if there aren't any. An alternative if you're happy to get null if there aren't any would be:

这将找到第一个路径元素,如果没有,将抛出NullReferenceException。另一种方法是,如果你很高兴,如果没有的话,你将会得到null:

string filePath = (string) fileElement.Element("Path");

If you're really stuck with XNode, you'll either have to cast to XElement or possibly XContainer.

如果您真的被XNode卡住了,那么您将不得不转换到XElement或XContainer。

#2


7  

You can convert your XNode into XElement to access to its properties, my example:

您可以将XNode转换为XElement来访问它的属性,我的示例:

XNode lastNode = myXElement.LastNode;

//if I want to get the 'ID' attribute
string id = (lastNode as XElement).Attribute("ID").Value;

#3


0  

You may use this:

你可以用这个:

XElement xtr = XElement.Load("path/to/your/xml/file");
String filePath = xtr.Descendants("Path").Single().Value;

#4


0  

If you import System.Xml.XPath you can use XPathSelectElement like this on the XNode object:

如果你进口System.Xml。XPath可以在XNode对象上使用XPathSelectElement:

String component = xmlNode.XPathSelectElement("Component");
String path = xmlNode.XPathSelectElement("Path");
String fileName = xmlNode.XPathSelectElement("FileName");

#5


0  

Casting XNode to XElement works for the individual element to retrieve its value or attributes. But you won't be able to use myXelement.Elements("XXX") to get nested elements. For that you can use xmlNode.Nodes().

将XNode转换为XElement为单个元素工作,以检索其值或属性。但是,您将无法使用myXelement.Elements(“XXX”)来获取嵌套元素。为此,可以使用xmlNode.Nodes()。

This should work:

这应该工作:

var nodes = xmlNode.Nodes();//Get all nodes under 'File'
var fileNameNode = nodes.Where(el => ((XElement)el).Name.LocalName == "FileName")
.FirstOrDefault();
string filePath = ((XElement)fileNameNode).Value;