XPath encounters a namespace

Source: Internet
Author: User

I have always been accustomed to using regular expressions to process text, including XML files. I only occasionally use XmlDocument and XPath.

Recently, the migration of the SP to MISC system was upgraded from 1.5 to 1.6. Its version 1.5 uses simple XML format responses and requests. The current version 1.6 uses standard SOAP information, A standard MISC request to SP may be like this: <? Xml version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: Envelope
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: xsd = "http://www.w3.org/2001/XMLSchema"
Xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV: Header>
<TransactionID xmlns = "http://www.monternet.com/dsmp/schemas/"> 06070516230084 </TransactionID>
</SOAP-ENV: Header>
SOAP-ENV: Body>
<SyncOrderRelationReq xmlns = "http://www.monternet.com/dsmp/schemas/">
<Version> 1.5.0 </Version>
<MsgType> SyncOrderRelationReq </MsgType>
<Send_Address>
<DeviceType> 0 </DeviceType>
<DeviceID> 0011 </DeviceID>
</Send_Address>
<Dest_Address>
<DeviceType> 400 </DeviceType>
<DeviceID> 0 </DeviceID>
</Dest_Address>
<FeeUser_ID>
<UserIDType> 1 </UserIDType>
<MSISDN> 13800138000 </MSISDN>
<Pseudo code>
</Pseudo code>
</FeeUser_ID>
<DestUser_ID>
<Useridtype> 1 </useridtype>
<Msisdn> 13800138000 </msisdn>
<Pseudo code> </pseudo code>
</Destuser_id>
<Linkid> linkid </linkid>
<Actionid> 1 </actionid>
<Actionreasonid> 1 </actionreasonid>
<SPID> 900000 </SPID>
<SPServiceID> abcd </SPServiceID>
<AccessMode> 3 </AccessMode>
<FeatureStr> </FeatureStr>
</SyncOrderRelationReq>
SOAP-ENV: Body>
SOAP-ENV: Envelope>

Haha, this is a big headache. Not only does the SOAP Standard define multiple namespaces, but Zhuo Wang also defines a namespace for mobile (however, the Schema is not actually provided ).
SelectNodes for such XML with namespaces, you can no longer use a simple XPath statement:

String xml = "";
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. LoadXml (xml );
XmlDoc. SelectNodes ("// SOAP-ENV: Header/TransationID"); // No node is available

In this case, you need to define the namespace one by one: string xml = "";
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. LoadXml (xml );
XmlNamespaceManager xnm = new XmlNamespaceManager (xmldoc. NameTable );
Xnm. AddNamespace ("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope ");
Xnm. AddNamespace ("Mo", "http://www.monternet.com/dsmp/schemas ");
Xmldoc. selectnodes ("// SOAP-ENV: Header/Mo: transationid", xnm); // note that you need to define a name for the namespace even if you use a node of the default namespace, and use this name.

I feel that there are fewer developers using XPath and xstl.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.