简体   繁体   English

为什么 windows 通用应用程序中没有 XmlNode.SelectSingleNode 方法?

[英]Why isn't there a XmlNode.SelectSingleNode method in windows universal app?

I got a Xml and i want to get single elements by their name.我有一个 Xml,我想通过他们的名字来获取单个元素。 I tried to use the SelectSingelNode-method.我尝试使用 SelectSingelNode 方法。 This is what MSDN tells you to do:这是 MSDN 告诉您要做的:

https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectsinglenode%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectsinglenode%28v=vs.110%29.aspx

At the moment I'm using XmlDocument and XmlNodeList to read the Xml.目前我正在使用 XmlDocument 和 XmlNodeList 来读取 Xml。 but this gives me the whole tree.但这给了我整棵树。

string path = "xml_path.xml";
FileStream reader = new FileStream(path, FileMode.Open, FileAccess.Read);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(reader);
XmlNodeList node = xdoc.GetElementsByName("name");

I can't find the SelectSingeNode method in win-universal-app.我在 win-universal-app 中找不到 SelectSingeNode 方法。 i am using Visual Studio 2015. Why did they remove this?我正在使用 Visual Studio 2015。他们为什么要删除它? Is there another way of getting a single element by it's name?有没有另一种方法可以通过名称获取单个元素?

XmlNodeList is Enumerable but it does not implement Generic IEnumerable so you have to Cast it before you can use Linq query to solve your problem XmlNodeList 是 Enumerable 但它没有实现 Generic IEnumerable 所以你必须先转换它才能使用 Linq 查询来解决你的问题

XmlNode node = xdoc.GetElementsByTagName("name").Cast<XmlNode>().First();
XmlNode node = xdoc.GetElementsByTagName("name").Cast<XmlNode>().FirstorDefault();
XmlNode node = xdoc.GetElementsByTagName("name").Cast<XmlNode>().Where(somecondition).FirstorDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM