Heim >Backend-Entwicklung >C++ >Wie kann ich mithilfe von LINQ to XML die GUID korrekt aus dem Attribut „ActivityID' in einem XML-Dokument mit Namespaces extrahieren?

Wie kann ich mithilfe von LINQ to XML die GUID korrekt aus dem Attribut „ActivityID' in einem XML-Dokument mit Namespaces extrahieren?

Barbara Streisand
Barbara StreisandOriginal
2025-01-04 19:25:41681Durchsuche

How can I correctly extract the GUID from the

LINQ to XML mit Namespaces in XDocuments

Das Abfragen von XML mit Namespaces in XDocuments kann eine Herausforderung sein. Betrachten Sie das folgende 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>

Der folgende Code extrahiert die GUID nicht aus dem „ActivityID“-Attribut:

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;

Lösung:

Um mit Namespaces zu arbeiten, verwenden Sie den folgenden Code:

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;

Complete Source for Testversion:

        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();
        }

Das obige ist der detaillierte Inhalt vonWie kann ich mithilfe von LINQ to XML die GUID korrekt aus dem Attribut „ActivityID' in einem XML-Dokument mit Namespaces extrahieren?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn