XDocuments의 네임스페이스를 사용하는 LINQ to XML
XDocuments의 네임스페이스를 사용하여 XML을 쿼리하는 것은 어려울 수 있습니다. 다음 XML을 고려하십시오.
<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>
다음 코드는 "ActivityID" 속성에서 GUID를 추출하지 않습니다.
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;
해결책:
네임스페이스 작업을 하려면 다음을 사용하세요. 코드:
XNamespace nsSys = "http://schemas.microsoft.com/2004/06/windows/eventlog/system"; XElement xEl2 = xDoc.Element(nsSys + "System"); XElement xEl3 = xEl2.Element(nsSys + "Correlation"); XAttribute xAtt1 = xEl3.Attribute("ActivityID"); String sValue = xAtt1.Value;
전체 평가판 소스:
public static void Main() { XElement xDoc = XElement.Parse( @"<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>"); XNamespace nsSys = "http://schemas.microsoft.com/2004/06/windows/eventlog/system"; XElement xEl2 = xDoc.Element(nsSys + "System"); XElement xEl3 = xEl2.Element(nsSys + "Correlation"); XAttribute xAtt1 = xEl3.Attribute("ActivityID"); String sValue = xAtt1.Value; Console.WriteLine("sValue = {0}", sValue); Console.ReadKey(); }
위 내용은 LINQ to XML을 사용하여 네임스페이스가 있는 XML 문서의 'ActivityID' 특성에서 GUID를 올바르게 추출하려면 어떻게 해야 합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!