- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我需要读取一个具有属性 xmlns="http://www.w3.org/2000/09/xmldsig#"的 xml 元素。XPathSelectElement 给出错误“对象引用未设置到对象的实例。”
这是示例代码。
var xml = "<root><tagA>Tag A</tagA><tagB>Tag B</tagB></root>";
XDocument xd = XDocument.Parse(xml);
var str = xd.XPathSelectElement("/root/tagB").ToString(SaveOptions.DisableFormatting);
Console.WriteLine(str);
上面代码的结果是:
<tagB>Tag B</tagB>
如果我输入属性,
var xml = "<root><tagA>Tag A</tagA><tagB xmlns=\"http://www.w3.org/2000/09/xmldsig#\">Tag B</tagB></root>";
我遇到了错误。
Object reference not set to an instance of an object.
我是不是漏掉了什么?谁能帮帮我。 (我知道我可以通过使用其他方法获得。我只想知道我在这里缺少什么)
非常感谢。
最佳答案
您可以在 XmlNamespaceManager 中注册元素的命名空间:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "http://www.w3.org/2000/09/xmldsig#");
var str = xd.XPathSelectElement("/root/ns:tagB", nsmgr)
.ToString(SaveOptions.DisableFormatting);
Console.WriteLine(str);
关于C# XPathSelectElement 和 xml with attribute xmlns ="http://www.w3.org/2000/09/xmldsig#"Help,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704093/
我们在我支持的应用程序中得到了意想不到的结果,因为代码在匹配多个项目的路径上调用 XPathSelectElement()。举例说明: XElement e = XElement.Parse(@"
我正在使用 XPathSelectElement 将数据存储到 XML 文件中。我如何在下面的代码片段中使用全局变量名称而不是 ID 名称“City1”? 我想要的代码是: string myVari
我有以下 XPath 字符串 "(//DEAL_SETS/DEAL_SET/DEALS/DEAL/PARTIES/PARTY[ROLES/ROLE/ ROLE_DETAIL/PartyRole
您好,不确定这是否可以完成,但我知道这里有人会知道:) 使用 XElement oNodeEquip = xmlDoc.XPathSelectElement("//ItemAry/Item/Equip
如何在可移植类库中使用 XPathSelectElement?当我尝试在项目中包含 System.Xml.XPath 程序集时,出现以下错误: The type 'System.Xml.Linq.XE
我想知道使用简单查询时是否存在任何性能差异: var x = document.XPathSelectElement("actors/actor") vs var x = document.Desce
我有如下的Xelement。 XXXXX http://open.fda.gov/license 2015-05-31 Oxygen
我正在从 xml 文档中提取一个元素,但它返回了 null OK 1439379003 我正在尝试提取 test2 元素,但它返回 null var responseXdoc = XDo
我在读取一些 xml 行时遇到了 XPathSelectElement 的问题.... 函数 handleEachSession 从我的其他函数的 mathod childList.ForEach()
为什么这个 Xpath 在使用 XDocument.XPathSelectElement 时不起作用? Xpath: //Plugin/UI[1]/PluginPageCategory[1]/Page
我有以下 xml 我已经尝试了所有可能的 XPath 查询组合来读取 db2 (="Name2") 的名称,但从未获得预期的结果。 到目前
我正在尝试使用 System.Xml.XPath 命名空间的 XPathSelectElement 方法,但由于某种原因它总是返回 null,我不知道为什么。 代码如下: TextReader str
为什么这个 Xpath 在使用 XDocument.XPathSelectElement 时不起作用? Xpath: //Plugin/UI[1]/PluginPageCategory[1]/Page
我有一个 XML 例如
我正在使用 XElement 创建内存中的 Xml 树。这是我的 xml 示例: createCurve
我有一个文件,基本上是一个 XPath 列表,如下所示: /Options/File[1]/Settings[1]/Type[1] /Options/File[1]/Settings[1]/Path[
怎么做node.XPathSelectElement()和 node.XPathSelectElements() behave when the selection is either not a N
我正在尝试弄清楚为什么我的 xpath 不会选择我指定的节点 我的 xpath 表达式是 //DefaultValue,所以我希望所有名为 DefaultValue 的元素都被选中 我的测试文件(缩减
在.Net 4.X中,这个扩展方法在System.Xml.XPath下。 但是我想知道如何在 .Net Core 中找到/使用它?我有一个类库。 移植工具不显示有关 XPathSelectElemen
我需要读取一个具有属性 xmlns="http://www.w3.org/2000/09/xmldsig#"的 xml 元素。XPathSelectElement 给出错误“对象引用未设置到对象的实例
我是一名优秀的程序员,十分优秀!