Search code examples
c#xmlxml-parsingvisual-studio-2015win-universal-app

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?


Solution

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