给出以下xml:
<ns0:MCCI_IN000002UV01 xmlns:ns0="urn:hl7-org:v3">
<ns0:id root="2.16.840.1.113883.3.277.100.1" extension="68423f2b-397a-4de4-8b8d-ea1f6c174954" />
<ns0:creationTime>201410171106-0700</ns0:creationTime>
<ns0:versionCode code="Ballot2009May" />
<ns0:interactionId root="2.16.840.1.113883.1.6" extension="MCCI_IN000002UV01" />
<ns0:processingCode code="P" />
<ns0:processingModeCode code="T" />
<ns0:receiver nullFlavor="NA">
<ns0:device nullFlavor="NA" classCode="DEV" determinerCode="INSTANCE">
<ns0:id nullFlavor="NA" />
</ns0:device>
</ns0:receiver>
<ns0:sender nullFlavor="NA">
<ns0:device nullFlavor="NA" classCode="DEV" determinerCode="INSTANCE">
<ns0:id nullFlavor="NA" />
</ns0:device>
</ns0:sender>
<ns0:acknowledgement typeCode="CA">
<ns0:targetMessage>
<ns0:id root="2.16.840.1.113883.3.277.100.1" extension="adb32b05-bf62-4417-8c62-d37a65380c4f" />
</ns0:targetMessage>
<ns0:acknowledgementDetail typeCode="I" />
</ns0:acknowledgement>
</ns0:MCCI_IN000002UV01>
我无法使用BizTalks XPathMutatorStream类查询hl7:MCCI_IN000002UV01 / hl7:versionCode / @ code属性,除非我更改了xml并删除了名称空间前缀。例如,xml现在看起来像这样:
<MCCI_IN000002UV01 xmlns="urn:hl7-org:v3">
<id root="2.16.840.1.113883.3.277.100.1" extension="68423f2b-397a-4de4-8b8d-ea1f6c174954" />
...
</MCCI_IN000002UV01>
不幸的是我无法更改xml,所以我必须处理ns0前缀。
基本上,我通过传递一个流来创建一个XMLReader对象:
XmlReader xr = XmlReader.Create(strMyStream);
然后我使用XPathExpression创建我的XPathCollection:
XPathCollection xc = new XPathCollection();
xc.NamespaceManager = new XmlNamespaceManager(xr.NameTable);
xc.NamespaceManager.AddNamespace("hl7", "urn:hl7-org:v3");
xc.Add(new XPathExpression("hl7:MCCI_IN000002UV01/hl7:versionCode/@code"));
我将XPathCollection和XmlReader实例传递给BizTalk XPathMutatorStream对象:
XPathMutatorStream str = new XPathMutatorStream(xr, xc, ...);
如果xml上没有名称空间前缀,这一切都可以正常工作,但只要有,我就永远不会得到任何匹配。我需要在命名空间管理器上执行某些操作,还是在实际的xpath语句中进行匹配?
答案 0 :(得分:2)
您是否尝试使用local-name()函数?
例如:// * [local-name()='MCCI_IN000002UV01'] / * [local-name()='versionCode'] / @ code
答案 1 :(得分:0)
如果你想使用Linq2Xml:
var xDoc = XDocument.Load(filename);
1 - 使用XPath
XmlNamespaceManager mgr = new XmlNamespaceManager(xDoc.CreateNavigator().NameTable);
mgr.AddNamespace("hl7", "urn:hl7-org:v3");
var version = xDoc.XPathSelectElement("hl7:MCCI_IN000002UV01/hl7:versionCode", mgr);
var code = version.Attribute("code").Value;
2 - 使用Linq
XNamespace h17 = "urn:hl7-org:v3";
var code2 = xDoc.Descendants(h17 + "versionCode").First().Attribute("code").Value;