复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<userSet>
<userInfo>
<profile>
<phoneNumber>13818181818</phoneNumber>
<country>China</country>
</profile>
</userInfo>
<userInfo>
<profile>
<phoneNumber>13919191919</phoneNumber>
<country>Korea</country>
</profile>
</userInfo>
</userSet>
复制代码 代码如下:
private void Test1()
{
XDocument xdoc = XDocument.Load(@"UserSet.xml");
var users = from u in xdoc.Descendants("userInfo")
where u.Attribute("id").Value == "1"
select u;
foreach (var u in users)
{
string name = u.Attribute("name").Value;
Console.WriteLine(name);
}
}
复制代码 代码如下:
private void Test2()
{
XDocument xdoc = XDocument.Load(@"UserSet.xml");
var users = from u in xdoc.Root.Elements("userInfo")
where u.Element("profile").Element("phoneNumber").Value == "13919191919"
select u;
foreach (var u in users)
{
string name = u.Attribute("name").Value;
Console.WriteLine(name);
}
}
新闻热点
疑难解答
图片精选