SelectSingleNode is giving compilation error in dn

2019-02-23 15:57发布

I am getting this error with SelectSingleNode method: DNX Core 5.0 error CS1061: 'XmlDocument' does not contain a definition for 'SelectSingleNode' and no extension method 'SelectSingleNode' accepting a first argument of type 'XmlDocument' could be found (are you missing a using directive or an assembly reference?)

Is it not supported yet? What are my alternatives?

3条回答
太酷不给撩
2楼-- · 2019-02-23 16:28

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);  
}
查看更多
闹够了就滚
3楼-- · 2019-02-23 16:41

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.

查看更多
叛逆
4楼-- · 2019-02-23 16:53

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" />
查看更多
登录 后发表回答