我有下面的XML而我試圖用的XDocument查詢:爲xEl1 包含的XDocument命名空間
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>589828</EventID>
<Type>3</Type>
<SubType Name="Information">0</SubType>
<Level>8</Level>
<TimeCreated SystemTime="2010-06-01T09:45:15.8102117Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" />
<Execution ProcessName="w3wp" ProcessID="5012" ThreadID="5" />
<Channel />
<Computer>TESTSERVER3A</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information">
<TraceIdentifier>http://msdn.microsoft.com/en-GB/library/System.ServiceModel.Activation.WebHostCompilation.aspx</TraceIdentifier>
<Description>Webhost compilation</Description>
<AppDomain>/LM/W3SVC/257188508/Root-1-129198591101343437</AppDomain>
<Source>System.ServiceModel.Activation.ServiceParser/39498779</Source>
<ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/StringTraceRecord">
<VirtualPath>/Service.svc</VirtualPath>
</ExtendedData>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>
執行以下代碼返回null除非我手動刪除命名空間:
XDocument xDoc = XDocument.Parse(CurrentString);
XElement xEl1 = xDoc.Element("E2ETraceEvent");
XElement xEl2 = xEl1.Element("System");
XElement xEl3 = xEl2.Element("Correlation");
XAttribute xAtt1 = xEl3.Attribute("ActivityID");
String sValue = xAtt1.Value;
你如何編寫代碼來提取XDocument中的Guid?
命名空間是關鍵 – ChrisP 2013-10-24 22:55:27
直到我意識到他們重寫了'XNamespace'和'string'的+運算符,這對我來說完全沒有任何意義。 Yucky噁心的惡作劇。 – 2013-12-23 09:57:19
如果你想避免「+」運算符重載,你可以顯式地從本地名稱和命名空間創建XName,如下所示:'XName.Get(「System」,nsSys.NamespaceName)' – Coruscate5 2016-11-29 17:29:10