简体   繁体   中英

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. I tried to use the SelectSingelNode-method. This is what MSDN tells you to do:

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. 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. i am using Visual Studio 2015. Why did they remove this? 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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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