包含命名空间的 XDocument

发布于 2024-09-04 23:04:30 字数 2209 浏览 6 评论 0原文

我尝试使用 XDocument 查询以下 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>

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

I have the following XML which I am trying to query with 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>

Executing the following code returns null for xEl1 except when I manually remove the namespaces:

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;

How do you write code to extract the Guid in XDocument?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秉烛思 2024-09-11 23:04:30

试试这个,对我有用

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

Try this, works for me

    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;

You need to use Namespaces.

Full source for trial

        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();
        }
无妨# 2024-09-11 23:04:30

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");

请注意命名空间如何为属性继承。

正如我在这里所做的那样,使用单个语句有时可以使生活变得更简单,但它确实使调试变得更加困难,因为您看不到中间值。根据口味修改:)

Anthony's addressed the namespace bit - and XAttribute has an explicit conversion to GUID, so this should work:

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");

Note how namespaces are not inherited for attributes.

Using a single statement as I've done here can sometimes make life simpler, but it does make debugging harder as you don't get to see the intermediate values. Modify according to taste :)

囍孤女 2024-09-11 23:04:30

在查询中使用 XNamespace 对象来查找要查询的元素中给定的“xmnls=”标记。还没有测试过,但类似的东西应该可以工作

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");

Use XNamespace objects in your query for the given "xmnls=" tags in the elements you wish to query. Haven't tested, but something like this should work

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文