[英]How to Parse XML Descendants elements subtree
這個XML有點不同,每個節點最初都是<dp41:nnnnn
(即它們的類型),並且':'是無效的,所以我刪除了這些,留下了。 解析它時,它將返回整個元素和子樹。 因此,問題是如何從此xml獲取元素(節點)。 所使用的代碼將整個子樹和指定的父節點作為一個元素返回,即來自(包括).. VehicleValueInfo ... +中的所有元素以及關閉標記VehicleValueInfo />想要的是VehicleValue'節點的后代元素。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Body>
<GetConvergedDataRequestResponse xmlns="http://autoi.trnsn.co.za/types" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ConvergedData i:type="d4p1:ConvergedResults" xmlns:="http://schemas.datacontract.org/2004/07/Trnsn.Auto.Convergence.B2B.BusinessModels">
<AccidentHistory i:nil="true"/>
<AlertInfo i:nil="true"/>
<CloneInfo i:nil="true"/>
<DiskDriveInfo>
<ResultCode i:nil="true"/>
<ResultCodeDescription i:nil="true"/>
<AirbagDetails>DRIVER, PASSENGER</AirbagDetails>
</DiskDriveInfo>
<EnquiryHistory i:nil="true"/>
<VehicleValueInfo>
<VehicleValue>
<ResultCode i:nil="true"/>
<AdjCostPrice>0</AdjCostPrice>
<VehicleCode>60007400</VehicleCode>
</VehicleValue>
</VehicleValueInfo>
代碼是;
XNamespace ns = "http://autoi.trnsn.co.za/types";
var xml = XDocument.Parse(InXML);
foreach (XElement element in xml.Descendants("{" + ns + "}VehicleValue"))
{
Console.WriteLine(element.ToString());
};
無論第一個foreach中的后代或元素或foreach如何,輸出都仍然是整個樹(整體而言):
<VehicleValue xmlns="http://autoinsight.transunion.co.za/types">
<ResultCode i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
<ResultCodeDescription i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
<AdjCostPrice>0</AdjCostPrice>
..
..
<VehicleCode>60007400</VehicleCode>
</VehicleValue>
我在這里添加整個僅需XML
<?xml version="1.0" encoding="UTF-8"?>
-<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
-<s:Body>
-<GetConvergedDataRequestResponse xmlns="http://autoinsight.trann.co.za/types" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
-<ConvergedData i:type="ConvergedResults" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Trann.Auto.Convergence.B2B.BusinessModels">
<AccidentHistory i:nil="true"/>
<AlertInfo i:nil="true"/>
<CloneInfo i:nil="true"/>
-<DiskDriveInfo>
<ResultCode i:nil="true"/>
<ResultCodeDescription i:nil="true"/>
<AirbagDetails>DRIVER, PASSENGER</AirbagDetails>
<Alarm>NO</Alarm>
</DiskDriveInfo>
<EnquiryHistory i:nil="true"/>
<FactoryFittedExtras i:nil="true"/>
<Finance i:nil="true"/>
<MileageHistory i:nil="true"/>
-<VehicleCodeAndDescription>
<ResultCode i:nil="true"/>
<ResultCodeDescription i:nil="true"/>
<VehicleCode>60007400</VehicleCode>
</VehicleCodeAndDescription>
<VehicleConfirmationInfo i:nil="true"/>
-<VehicleValueInfo>
-<VehicleValue>
<ResultCode i:nil="true"/>
<ResultCodeDescription i:nil="true"/>
<AdjustCostPrice>0</AdjustCostPrice>
<AdjEstCostPrice>0</AdjEstCostPrice>
<CostPrice>0</CostPrice>
<TradePrice>0</TradePrice>
<VehicleCode>60007400</VehicleCode>
</VehicleValue>
</VehicleValueInfo>
<VesaInfo i:nil="true"/>
</ConvergedData>
<ResponseStatus xmlns:d4p1="http://schemas.servicestack.net/types" i:nil="true"/>
</GetConvergedDataRequestResponse>
</s:Body>
</s:Envelope>
嘗試這個
XNamespace ns = "http://autoi.trnsn.co.za/types";
var xml = XDocument.Parse(InXML);
foreach (XElement element in xml.Descendants(ns + "VehicleValue"))
{
foreach(XElement ele in element.Elements())
{
Console.WriteLine((string)ele);
}
};
您必須使用xml.Elements
而不是xml.Descendants
var xml = XDocument.Parse(InXML);
foreach (XElement element in xml.Elements("VehicleValue"))
{
Console.WriteLine(element.ToString());
};
后代返回起始元素下的所有樹
元素返回起始元素的所有直接子元素
如果不需要名稱空間,則可以采用以下解決方案:
var xml = XDocument.Parse(InXML);
xml.RemoveNamespace();
foreach (XElement element in xml.Descendants("VehicleValue").Elements())
{
Console.WriteLine(element.ToString());
};
其中RemoveNamespace()
是:
public static void RemoveNamespace(this XContainer pContainer)
{
pContainer.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();
foreach (var element in pContainer.Descendants())
element.Name = element.Name.LocalName;
}
順便說一句,看來xml在第5行第69位的格式不正確。我想這里缺少名稱空間前綴聲明...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.