【发布时间】:2017-03-30 20:31:10
【问题描述】:
我在使用 SelectSingleNode 方法时遇到此错误: DNX Core 5.0 错误 CS1061:“XmlDocument”不包含“SelectSingleNode”的定义,并且找不到接受“XmlDocument”类型的第一个参数的扩展方法“SelectSingleNode”(您是否缺少 using 指令或程序集引用?)
还不支持吗?我的替代方案是什么?
【问题讨论】:
我在使用 SelectSingleNode 方法时遇到此错误: DNX Core 5.0 错误 CS1061:“XmlDocument”不包含“SelectSingleNode”的定义,并且找不到接受“XmlDocument”类型的第一个参数的扩展方法“SelectSingleNode”(您是否缺少 using 指令或程序集引用?)
还不支持吗?我的替代方案是什么?
【问题讨论】:
在 .Net Core 1.0 和 .Net Standard 1.3 中,SelectSingleNode 是一种扩展方法
https://github.com/dotnet/corefx/issues/17349
添加引用以使其再次可用:
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
【讨论】:
你需要使用 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);
}
【讨论】:
我也有这个问题。为了解决这个问题,我正在使用 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;
}
不要忘记在 project.json 中包含 "System.Xml.XDocument"。
【讨论】: