SelectSingleNode gives compilation error in dnx core 5.0

I get this error using the SelectSingleNode method: DNX Core 5.0 CS1061 error: "XmlDocument" does not contain a definition for "SelectSingleNode", and the "SelectSingleNode" extension method cannot be found that takes the first argument of type "XmlDocument" (you are missing the using directive or assembly links?)

Isn't it supported yet? What are my alternatives?

+4
source share
3 answers

In .Net Core 1.0 and .Net Standard 1.3 SelectSingleNode - extenstion method

https://github.com/dotnet/corefx/issues/17349

Add the link to make it available again:

 <PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
+2
source

You need to use XDocument

const string xml = "<Misc><E_Mail>email@domain.xyz</E_Mail><Fax_Number>111-222-3333</Fax_Number></Misc>";
const string tagName = "E_Mail";
XDocument xDocument = XDocument.Parse(xml);
XElement xElement = xDocument.Descendants(tagName).FirstOrDefault();
if (xElement == null)
{
    Console.WriteLine($"There is no tag with the given name '{tagName}'.");
}
else
{
    Console.WriteLine(xElement.Value);  
}
+1

. , XDocument .

:

XDocument xdoc = XDocument.Parse(xmlText);
var singleNode = xdoc.Element("someAttr");
var listOfNodes = singleNode.Elements("someAttrInnerText");

foreach (XElement e in listOfNodes)
{
     string someAttr = e.Attribute("code").Value;
     string someAttrInnerText = e.Value;
}

Remember to include "System.Xml.XDocument"in your project.json .

0
source

Source: https://habr.com/ru/post/1626563/


All Articles