鑒于此 XML,我如何獲取每個 outageEvent 的資料,包括屬性和元素?
最后,我需要將這些資料移動到資料庫中,但這是第一步。
所需的結束格式是每個帶有屬性 ObjectID 的 outageEvent 的“行”,然后是 outageEvent 中的元素。
outageEvent
-------------
ObjectID
Comment
objectName
...etc.
此時不需要獲取 outageEvent 的孫子節點,例如 ExtensionList 子節點。
到目前為止,我正在使用 Linq to XML 進行查詢。我能夠查詢并獲取 objectID。另外,我可以查詢以獲取所有元素。試圖將兩者結合起來給我帶來了問題。有一段時間沒有做任何 C#,所以這是 99% 的問題。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
</soap:Header>
<soap:Body>
<OutageEventChangedNotification xmlns="http://www.multispeak.org/Version_4.1_Release">
<oEvents>
<outageEvent objectID="2021-10-16-0002">
<comments>Test outage 2</comments>
<extensionsList>
<extensionsItem>
<extName>primaryCrew</extName>
<extValue>Primary Crew Name</extValue>
<extType>string</extType>
</extensionsItem>
</extensionsList>
<objectName>Outage Name</objectName>
<!-- more elements -->
</outageEvent>
<outageEvent objectID="2021-10-16-0001">
<comments>Test outage 1</comments>
<!-- more elements -->
</outageEvent>
</oEvents>
</OutageEventChangedNotification>
</soap:Body>
</soap:Envelope>
C#代碼:
XElement outages = XElement.Parse(requestBody);
XNamespace oecn = "http://www.multispeak.org/Version_4.1_Release";
//This works
IEnumerable<string> outageIDs = from outage in outages.Descendants(oecn "outageEvent")
select (string) outage.Attribute("objectID");
foreach (string outageID in outageIDs)
{
Console.WriteLine($"IENUMBERABLE: {outageID}");
}
//This works
IEnumerable<string> outageProperties = from outage in outages.Descendants(oecn "outageEvent")
select (string)outage;
foreach (string outageProperty in outageProperties)
{
Console.WriteLine($"IENUMBERABLE: {outageProperty} \r\n");
}
//This Doesn't
var alloutages = from outage in outages.Descendants(oecn "outageEvent")
select new /*Error Here*/
{
outageID = outage.Attribute("objectID").Value,
comments = outage.Element("comments").Value
};
foreach (var outage in alloutages)
{
Console.WriteLine($"OUTPUT: {outage.outageID}");
}
輸出:
Hello World!
IENUMBERABLE: 2021-10-16-0002
IENUMBERABLE: 2021-10-16-0001
IENUMBERABLE: Test outage 2primaryCrewPrimary Crew NamestringOutage Name0032.427326-99.788595TRANSFORMER1District9TRANSFORMER1TransformerA10FeederNameAssumed2015-01-01T10:00:00Z2015-01-01T10:30:00Z2015-01-01T11:00:00ZCrew1Crew21010Contractor44
IENUMBERABLE: Test outage 1primaryCrewPrimary Crew NamestringOutage Name0032.427326-99.788595TRANSFORMER1District9TRANSFORMER1TransformerA10FeederNameAssumed2015-01-01T10:00:00Z2015-01-01T10:30:00Z2015-01-01T11:00:00ZCrew11010Contractor44
var = alloutages 部分出錯,突出顯示“選擇新”塊。
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=ParseXMLWithLinq
StackTrace:
at XMLParse.Program.<>c.<Main>b__0_2(XElement outage) in
System.Xml.Linq.XContainer.Element(...) returned null.
alloutages 的內容如下所示:
<outageEvent objectID="2021-10-16-0002" xmlns="http://www.multispeak.org/Version_4.1_Release">
<comments>Test outage 2</comments>
<extensionsList>
<extensionsItem>
<extName>primaryCrew</extName>
<extValue>Primary Crew Name</extValue>
<extType>string</extType>
...
</outageEvent>
uj5u.com熱心網友回復:
例外的原因是commentselement insideoutageEvent與 位于相同的命名空間中outageEvent,因此在http://www.multispeak.org/Version_4.1_Release. 所以而不是:
select new /*Error Here*/
{
outageID = outage.Attribute("objectID").Value,
comments = outage.Element("comments").Value
};
你需要做:
select new /*Error Here*/
{
outageID = outage.Attribute("objectID").Value,
// use namespace you already have here
comments = outage.Element(oecn "comments").Value
};
另一種方法是按本地名稱過濾元素:
select new /*Error Here*/
{
outageID = outage.Attribute("objectID").Value,
comments = outage.Elements().First(c => c.Name.LocalName == "comments").Value
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321716.html
標籤:C# 。网 xml linq-to-xml
