SelectSingleNode is giving compilation error in dnx core 5.0

主宰稳场 提交于 2019-12-01 19:39:26

In .Net Core 1.0 and .Net Standard 1.3 SelectSingleNode is an extenstion method

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

Add a reference to make it available again:

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

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

I have this problem too. To solve that, I'm using XDocument and so far so good.

Example:

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;
}

Don't forget to include "System.Xml.XDocument" inside your project.json.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!