C# 如何从XNode读取数据
如何从XNode读取数据 这是我从另一个查询中得到的XNodeC# 如何从XNode读取数据,c#,xml,linq,C#,Xml,Linq,如何从XNode读取数据 这是我从另一个查询中得到的XNode <claim kind="national" sequence="1"> <country>UK</country> <number>66</number> <date>20080602</date> </claim> <claim kind="national" sequence="3"> <count
<claim kind="national" sequence="1">
<country>UK</country>
<number>66</number>
<date>20080602</date>
</claim>
<claim kind="national" sequence="3">
<country>TH</country>
<number>61</number>
<date>20090316</date>
</claim>
英国
66
20080602
真实航向
61
20090316
我想得到国家和日期值
任何帮助都将不胜感激。
谢谢。试试看
var list = from ele in XDocument.Load(@"c:\file.xml").Descendants("claim")
select new
{
Country=(string)ele.Element("country"),
Date=(string)ele.Element("date")
};
foreach (var t in list)
{
Console.WriteLine(t.Country + " "+ t.Date );
}
编辑:
string country=(string)((XElement)xNodeObj).Element("country");
下面是一种使用XPath的方法:
string xml = "<main><claim kind=\"national\" sequence=\"1\"> <country>UK</country> <number>66</number> <date>20080602</date></claim><claim kind=\"national\" sequence=\"3\"> <country>TH</country> <number>61</number> <date>20090316</date></claim></main>";
XDocument doc = XDocument.Parse(xml);
doc.XPathSelectElement("//claim[country = 'TH']/number").Value.Dump("xpath stuff");
string xml=“UK 66 20080602 TH 61 20090316”;
XDocument doc=XDocument.Parse(xml);
doc.XPathSelectElement(“//claim[country='TH']/number”).Value.Dump(“xpath填充”);
谢谢您的回复。既然这个xml已经在XNode中(不是从文件中),我该怎么做呢?XDocument.degenders正是我想要将节点作为元素抓取的东西,干杯!foreach(XDocument.Parse(xmlString.substands(“过滤器”)中的XElement节点)