SelectSingleNode 和命名空间 - 找不到节点(使用命名空间 mgr)

发布于 2024-10-20 21:42:28 字数 1448 浏览 8 评论 0原文

我从合作伙伴公司收到一条相对复杂的 xml 消息。我需要解析 xml 才能使用它。我让它工作正常,然后他们改变了消息,现在我的 SelectSingleNode 命令都不起作用。

消息的结构。请注意带有嵌入 xml 文档的 CDATA 对象。不确定这是否重要。我能够取出 dataContent 对象,所以它应该不会产生影响。以下是我要加载到 XmlDocument 中的内容:

<ns5:dataContent xmlns:ns2="http://test/common/v1" 
                     xmlns="http://test/schema/common" 
                     xmlns:ns4="http://test/credentialing/stuff/v1" 
                     xmlns:ns3="http://test/schema/ims/common/v1" 
                     xmlns:ns5="http://test/schema/v1">

        <createdBy>Micky</createdBy>
        <createdAt>2011-03-08T17:00:27.050-05:00</createdAt>
        <ns5:Id>39</ns5:Id>
        <ns5:Type>4</ns5:Type>
     -- lots more data --
</ns5:dataContent>

我的代码相当简单,

   var xmlDoc = new XmlDocument();
   xmlDoc.Load(new StringReader(CDATA content));
   var xmlNsM = new XmlNamespaceManager(xmlData.NameTable);
   xmlNsM.AddNamespace(String.Empty, @"http://test/schema/common\");
   xmlNsM.AddNamespace("ns5", @"http://test/schema/v1\");
   xmlNsM.AddNamespace("m", @"http://test/message/v1\");

//This works
   var order = xmlDoc.ChildNodes[0];

//This returns null
   var ID = order.SelectSingleNode("ns5:Id", xmlNsM);

我可以看到 xmlDoc 正在正确加载。我尝试将所有 6 个命名空间添加到 xmlNsM,但得到相同的结果。我的 SelectSingleNodes 现在都不起作用。

我发现的关于这个主题的每一篇文章都说只使用命名空间管理器,但我已经知道这一点,所以这不是一个非常富有成效的下午。

I have a relativly complex xml message I am recieving from a partner company. I need to parse up the xml to use it. I had it working fine, then they changed the message all around and now none of my SelectSingleNode commands work.

Structure of the message. Note the CDATA object with an embedded xml document. Not sure this matters or not. I was able to tweeze out the dataContent object, so it shouldn't make a difference. Here is what I am loading into the XmlDocument:

<ns5:dataContent xmlns:ns2="http://test/common/v1" 
                     xmlns="http://test/schema/common" 
                     xmlns:ns4="http://test/credentialing/stuff/v1" 
                     xmlns:ns3="http://test/schema/ims/common/v1" 
                     xmlns:ns5="http://test/schema/v1">

        <createdBy>Micky</createdBy>
        <createdAt>2011-03-08T17:00:27.050-05:00</createdAt>
        <ns5:Id>39</ns5:Id>
        <ns5:Type>4</ns5:Type>
     -- lots more data --
</ns5:dataContent>

my code is fairly straight forward

   var xmlDoc = new XmlDocument();
   xmlDoc.Load(new StringReader(CDATA content));
   var xmlNsM = new XmlNamespaceManager(xmlData.NameTable);
   xmlNsM.AddNamespace(String.Empty, @"http://test/schema/common\");
   xmlNsM.AddNamespace("ns5", @"http://test/schema/v1\");
   xmlNsM.AddNamespace("m", @"http://test/message/v1\");

//This works
   var order = xmlDoc.ChildNodes[0];

//This returns null
   var ID = order.SelectSingleNode("ns5:Id", xmlNsM);

I can see that xmlDoc is being loaded properly. I have tried adding all 6 namespaces to the xmlNsM, but get the same results. None of my SelectSingleNodes works now.

Every post I have found on this subject says to just use the namespace manager, but I already knew that so its not been a very productive afternoon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住你手 2024-10-27 21:42:29

通常情况下,经过数小时的研究后,解决方案很简单。我添加到 NSManager 的命名空间在右引号前面有一个正斜杠(“http://test/schema/v1\”)。当然,这是一个分隔符,但由于我刚刚从原始 xml 中复制并粘贴了命名空间,因此它被包含在内。删除它们后,我开始在 ns5 命名空间中查找所需的所有字段。

由于某种原因,我看不到没有名称空间的元素,但由于我不需要这些字段 ATM,所以我稍后会解决这个问题。

Well as is typically the case after hours and hours of looking at the problem, the solution was simple. The namespaces I added to the NSManager had a forward slash in front of the closing quotation mark ("http://test/schema/v1\"). Of course this is a delimiter, but since I just copied and pasted the namespace from the raw xml it was included. Once I removed them, I started finding all the fields I needed in the ns5 namespace.

I can't see the elements that don't have namespaces for some reason, but since I don't need those fields ATM, I'll figure that out later.

云归处 2024-10-27 21:42:29

@ErnieL 答案看起来不错(+1)。也可能存在问题,

  xmlNsM.AddNamespace(String.Empty, @"http://test/schema/common\");

因为 xpath不喜欢默认命名空间 :-(。如果 @ErnieL 答案仍然不起作用,请将其更改为

  xmlNsM.AddNamespace("default", @"http://test/schema/common\");

@ErnieL answer looks good (+1). There might also bee a problem is with

  xmlNsM.AddNamespace(String.Empty, @"http://test/schema/common\");

because xpath does not like defaultnamespaces :-(. If @ErnieL answer still doesnot work change it to

  xmlNsM.AddNamespace("default", @"http://test/schema/common\");
半仙 2024-10-27 21:42:28

只是阅读代码,我认为您正确使用了命名空间管理器。您的问题是 XPath 查询。 “ns5:Id”不是高级文档的子级,ChildNodes 只会返回下一级文档。你需要寻找死者。尝试:

var root = doc.DocumentElement;
var ID = root.SelectSingleNode("//ns5:Id", xmlNsM); 

这不是最有效的,因为它会找到整个文档中的所有匹配项。看一下这里的一些示例: http://msdn.microsoft.com /en-us/library/ms256086.aspx

Just reading the code I think you are using the Namespace Manager correctly. Your problem is the XPath query. "ns5:Id" is not a child of the high level document and ChildNodes will only return one level down. You need to be searching decedents. Try:

var root = doc.DocumentElement;
var ID = root.SelectSingleNode("//ns5:Id", xmlNsM); 

This isn't the most efficient because it will find all matches in the entire document. Take a look at some of the examples here: http://msdn.microsoft.com/en-us/library/ms256086.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文