在c#中检索xml片段
本文关键字:xml 片段 检索 | 更新日期: 2023-09-27 18:18:00
我要为HB_Base元素检索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>
如何使用使用XmlNamespaceManager
的XmlDocument.SelectSingleNode()
。因为使用"/Envelope/Body/Car/carApplication/HB_Base"
您需要指定名称空间,使用XLinq
,我们将使用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
和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;
我的建议是使用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();
}
您可以使用XmlNode。OuterXml财产。
将其加载到xml文档中并选择节点,然后检索文本。
XmlDocument doc = new XmlDocument();
doc.loadXml( theString );
doc.SelectSingleNode( "/Envelope/Body/Car/carApplication/HB_Base" ).OuterXml;