返回的SelectSingleNode命名空间管理,甚至空 [英] SelectSingleNode returns null even with namespace managing

查看:284
本文介绍了返回的SelectSingleNode命名空间管理,甚至空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自这个问题,我的第一个问题解决了:
XML选择其中姓名是重复
这是一个命名空间问题,第一单节点。



但现在即使有corect名称空间管理我的XPath还是返回我空。



我也检查:



的SelectSingleNode返回NULL - 即使有空间
的SelectSingleNode始终返回null?
的XmlDocument。的SelectSingleNode和XML命名空间的问题
的SelectSingleNode使用XPath
为什么是的SelectSingleNode返回null?



但没有'时间帮助了我。我被困在这个问题上几个小时。 ?什么是错的任何帮助。



感谢



示例XML(编辑:FULL XML)



 <?XML版本=1.0编码=UTF-8>?; 
< JMF SenderID =InkZone - 控制器版本=1.2的xmlns =htt​​p://www.CIP4.org/JDFSchema_1_1>
<命令ID =cmd.00695TYPE =资源>
< ResourceCmdParams资源名称=InkZoneProfile作业ID =K_41>
< InkZoneProfile ID =R0013class =参数锁定=假状态=可用PartIDKeys =SignatureName SHEETNAME侧分离DescriptiveName =Schieberwerte冯DIZoneWidth =32>
< InkZoneProfile SignatureName =SIG1>
< InkZoneProfile锁定=假SHEETNAME =S1>
< InkZoneProfile侧=前>
< InkZoneProfile分离=设计师P和克1901年ZoneSettingsX =0.391/>

< / InkZoneProfile>
< / InkZoneProfile>
< / InkZoneProfile>
< / InkZoneProfile>
< / ResourceCmdParams>
< /指挥GT;
< / JMF>



我选择指定的XML节点代码:

 的XmlDocument xmlDoc中=新的XmlDocument(); 
xmlDoc.Load(C:\\XML\\test.xml);
XmlNode的根= xmlDoc.DocumentElement;
的XmlNamespaceManager nsmgr =新的XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace(CIP4NS,http://www.CIP4.org/JDFSchema_1_1);

VAR父= root.SelectSingleNode(// CIP4NS:命令/ ResourceCmdParams / InkZoneProfile / InkZoneProfile / InkZoneProfile / InkZoneProfile,nsmgr);
的XmlElement IZP = xmlDoc.CreateElement(InkZoneProfile);
IZP.SetAttribute(分离,x.colorname);
IZP.SetAttribute(ZoneSettingsX,x.colorvalues​​);
parent.AppendChild(IZP);
xmlDoc.Save(C:\\XML\\test.xml);


解决方案

您的XML具有的默认命名空间其后代元素没有前缀的继承隐的从祖先。这意味着,不仅根元素,但在的XPath中提到的所有元件都在同一个缺省名称空间,因此,需要通过使用相同的前缀引用



  // CIP4NS:命令/ CIP4NS:ResourceCmdParams / CIP4NS:InkZoneProfile / CIP4NS:InkZoneProfile / CIP4NS:InkZoneProfile / CIP4NS:InkZoneProfile 


I've come from this question, where my first issue was solved: XML Select a single node where the names are repeating It was a namespace issue first.

But now even with the corect NameSpace managing my XPath still returns me null.

I've also checked:

SelectSingleNode return null - even with namespace SelectSingleNode always returns null? XmlDocument.SelectSingleNode and xmlNamespace issue SelectSingleNode returning null for known good xml node path using XPath Why is SelectSingleNode returning null?

But none of 'em helped me. I'm stuck for some hours on this issue. What is wrong with it ?

Thanks for any help.

Sample XML(EDITED: FULL XML)

<?xml version="1.0" encoding="utf-8"?>
<JMF SenderID="InkZone-Controller" Version="1.2" xmlns="http://www.CIP4.org/JDFSchema_1_1">
    <Command ID="cmd.00695" Type="Resource">
        <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
            <InkZoneProfile ID="r0013" Class="Parameter" Locked="false" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
                <InkZoneProfile SignatureName="SIG1">
                    <InkZoneProfile Locked="false" SheetName="S1">
                        <InkZoneProfile Side="Front">
                            <InkZoneProfile Separation="designer P&G 1901" ZoneSettingsX="0.391 "/>

                        </InkZoneProfile>
                    </InkZoneProfile>
                </InkZoneProfile>
            </InkZoneProfile>
        </ResourceCmdParams>
    </Command>
</JMF>

My code for selecting the specified XML Node:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\XML\\test.xml");
XmlNode root = xmlDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("CIP4NS", "http://www.CIP4.org/JDFSchema_1_1");

var parent = root.SelectSingleNode("//CIP4NS:Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile", nsmgr);
XmlElement IZP = xmlDoc.CreateElement("InkZoneProfile");
IZP.SetAttribute("Separation", x.colorname);
IZP.SetAttribute("ZoneSettingsX", x.colorvalues);
parent.AppendChild(IZP);
xmlDoc.Save("C:\\XML\\test.xml");

解决方案

Your XML has default namespace which descendant elements without prefix implicitly inherits from the ancestor. That implies, not only the root element but all elements mentioned in your XPath are in the same default namespace, hence need to be referenced by using the same prefix :

//CIP4NS:Command/CIP4NS:ResourceCmdParams/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile

这篇关于返回的SelectSingleNode命名空间管理,甚至空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆