2011-12-16 29 views
1

我該如何檢索HB_Base元素的xml片段的字符串表示形式。任何人都可以指出最好的方法。檢索c#中的xml片段

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 
    <s:Header> 
    <a:Action s:mustUnderstand="1">http://www.as.com/ver/ver.IClaimver/Car</a:Action> 
    <a:MessageID>urn:uuid:b22149b6-2e70-46aa-8b01-c2841c70c1c7</a:MessageID> 
    <ActivityId CorrelationId="16b385f3-34bd-45ff-ad13-8652baeaeb8a" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">04eb5b59-cd42-47c6-a946-d840a6cde42b</ActivityId> 
    <a:ReplyTo> 
     <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address> 
    </a:ReplyTo> 
    <a:To s:mustUnderstand="1">http://localhost/ver.Web/ver2011.svc</a:To> 
    </s:Header> 
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Car xmlns="http://www.as.com/ver"> 
     <carApplication> 
     <HB_Base xsi:type="HB" xmlns="urn:core"> 
      <Header> 
      <Advisor> 
       <AdvisorLocalAuthorityCode>11</AdvisorLocalAuthorityCode> 
       <AdvisorType>1</AdvisorType> 
      </Advisor> 
      </Header> 
      <General> 
      <ApplyForHB>yes</ApplyForHB> 
      <ApplyForCTB>yes</ApplyForCTB> 
      <ApplyForFSL>yes</ApplyForFSL> 
      <ConsentSupplied>no</ConsentSupplied> 
      <SupportingDocumentsSupplied>no</SupportingDocumentsSupplied> 
      </General> 
     </HB_Base> 
     </carApplication> 
    </Car> 
    </s:Body> 
</s:Envelope> 

更新

如何使用XmlDocument.SelectSingleNode()使用XmlNamespaceManager。因爲它可能更好使用像"/Envelope/Body/Car/carApplication/HB_Base"

+0

如何閱讀XML。真的,答案更多地是關於您使用的特定XML解析技術,而不是XML本身。 – 2011-12-16 18:23:30

回答

1

你需要指定的命名空間,using XLinq we'd use an XNamespace,以幫助它指的是元素XName

var xdoc = XDocument.Load(xmlFile); // or .Parse(@"....") 

XNamespace ns = @"urn:core"; 
// This is where the magic happens: ns + "HB_Base" 
var hbBase = xdoc.Root.Descendants(ns + "HB_Base") 
         .SingleOrDefault(); 

if (hbBase == null) throw new InvalidOperationException("No such element"); 

var xml = hbBase.ToString(); 

使用XmlDocument and XmlNamespaceManager代替:

var xmlDoc = new XmlDocument(); 
xmlDoc.Load(xmlFile); 

var nsManager = new XmlNamespaceManager(xmlDoc.NameTable); 
nsManager.AddNamespace("s", @"http://www.w3.org/2003/05/soap-envelope"); 
nsManager.AddNamespace("c", @"http://www.as.com/ver"); 
nsManager.AddNamespace("h", @"urn:core"); 

var hbBase = xmlDoc.SelectSingleNode(
    @"/s:Envelope/s:Body/c:Car/c:carApplication/h:HB_Base"); 
if (hbBase == null) throw new InvalidOperationException("No such element"); 

var xml = hbBase.OuterXml; 
+0

謝謝,你的回答工作。 – Pingpong 2011-12-16 18:51:05

+0

如何使用使用XmlNamespaceManager的XmlDocument.SelectSingleNode()。因爲使用xpath如「/ Envelope/Body/Car/carApplication/HB_Base」可能會更好。 – Pingpong 2011-12-19 19:25:04

0

將其加載到xml文檔並選擇您的節點,然後回顧文本。這可能不會編譯。

XmlDocument doc = new XmlDocument(); 
doc.loadXml(theString); 
doc.SelectSingleNode("/Envelope/Body/Car/carApplication/HB_Base").OuterXml; 
+0

他需要將`urn:core`命名空間應用於該元素。 – user7116 2011-12-16 18:33:48

1

我的建議是使用LINQ to XML

using System.Linq; 
using System.Xml.Linq; // requires a reference to System.Xml.Linq.dll 

// . . . 

// Load the XML (in this example, we use a file) 
XDocument document = XDocument.Load("yourfile.xml"); 

// Initialize the namespace for the target element 
XNamespace coreNamespace = "urn:core"; 

// Choose the first element below the root matching our target element 
// (or return null if there is none) 
XElement chosenElement = document.Root.Descendants(coreNamespace + "HB_Base").FirstOrDefault(); 

string xmlString = null; 
if (chosenElement != null) 
{ 
    // We found it, now get the string representation of the XML 
    xmlString = chosenElement.ToString(); 
}