包含名称空间的XDocument

时间:2010-06-08 15:21:44

标签: c# xml linq linq-to-xml xml-namespaces

我有以下XML,我试图用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>

执行以下代码为xEl1 返回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?

3 个答案:

答案 0 :(得分:59)

试试这个,适合我

    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;

您需要使用Namespaces

试用的完整来源

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

答案 1 :(得分:22)

Anthony找到了命名空间位 - 而XAttribute显式转换为GUID,所以这应该有效:

XNamespace eventNs = "http://schemas.microsoft.com/2004/06/E2ETraceEvent";
XNamespace systemNs = "http://schemas.microsoft.com/2004/06/windows/eventlog/system";

Guid guid = (Guid) document.Element(eventNs + "E2ETraceEvent")
                           .Element(systemNs + "System")
                           .Element(systemNs + "Correlation")
                           .Attribute("ActivityID");

请注意名称空间 是如何为属性继承的。

我在这里使用单个语句有时可以简化生活,但 会使调试变得更难,因为您无法查看中间值。根据口味修改:)

答案 2 :(得分:2)

在查询中使用名称空间对象,以查找要查询的元素中的给定“xmlns =”标记。没有经过测试,但这样的事情应该有效

XNamespace eventSpace = "http://schemas.microsoft.com/2004/06/E2ETraceEvent";
XNamespace systemSpace = "http://schemas.microsoft.com/2004/06/windows/eventlog/system";

XElement eventElement = document.Element(eventSpace + "E2ETraceEvent");
XElement systemElement = eventElement.Element(systemSpace + "System");