开发者

Can't parse XML because of the attribute stuck on element

开发者 https://www.devze.com 2023-03-05 18:59 出处:网络
The problem is the ServiceDocument as the xmlns attribute on it. ---Preassigned XML System.Xml.XmlDocument xmlDoc = new XmlDocument();

The problem is the ServiceDocument as the xmlns attribute on it.

---Preassigned XML

  System.Xml.XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>
                    <ServiceDocument 
                      xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"
                      xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"
                   >
                    <BaseUri>
                       http://xxxx.xxxxx.net/xxx.1/
                   </BaseUri>
                  <ProfilesLink>
                     http://adf.apis.dds.net/af.1/
                 </ProfilesLink>
                <SignedInUser>
                       <Cid>
                           4433sfsdfgd
                       </Cid>
                      <Uri>
                          http://fd.apis.afdafd.net/V4.1/cid-xxxxx/adad
                      </Uri>
               </SignedInUser> 
               <StatusMessageLink>
                      http://psm.adfa.afd.net/dfa.1/
               </StatusMessageLink>开发者_运维知识库
            </ServiceDocument>"
            );
 // Response.Write(xmlDoc.InnerXml);

--//PARSE XML problem is below**

 Response.Write(xmlDoc.SelectSingleNode("/ServiceDocument/BaseUri").InnerXml);


You need to assign short aliases to the namespaces using the XMLNamespaceManager.

See this page for an example.

So, to solve your problem:

var xmlNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNamespaceManager.AddNamespace("ds", "http://schemas.microsoft.com/ado/2007/08/dataservices");
var result = xmlDoc.SelectNodes("/ds:ServiceDocument/ds:BaseUri", xmlNamespaceManager);
0

精彩评论

暂无评论...
验证码 换一张
取 消