Linq To Xml 在不知道确切结构的情况下搜索 XML

发布于 2024-11-19 04:36:19 字数 3122 浏览 4 评论 0原文

我将如何解析以下内容以获取 sql 消息 .Net SqlClient Data Provider?

我正在尝试使用 Linq to xml 来完成此操作,但我运气不佳。 xml 是递归的,所以我无法确定消息将出现在什么级别

有什么想法吗?

谢谢

<detail>
    <ErrorCode xmlns="http://www.microsoft.com/sql/reportingservices">rsProcessingAborted</ErrorCode>
    <HttpStatus xmlns="http://www.microsoft.com/sql/reportingservices">400</HttpStatus>
    <Message xmlns="http://www.microsoft.com/sql/reportingservices">An error has occurred during report processing.</Message>
    <HelpLink xmlns="http://www.microsoft.com/sql/reportingservices">http://go.microsoft.com/fwlink/?LinkId=20476&amp;EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&amp;EvtID=rsProcessingAborted&amp;ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&amp;ProdVer=10.50.1600.1</HelpLink>
    <ProductName xmlns="http://www.microsoft.com/sql/reportingservices">Microsoft SQL Server Reporting Services</ProductName>
    <ProductVersion xmlns="http://www.microsoft.com/sql/reportingservices">10.50.1600.1</ProductVersion>
    <ProductLocaleId xmlns="http://www.microsoft.com/sql/reportingservices">1033</ProductLocaleId>
    <OperatingSystem xmlns="http://www.microsoft.com/sql/reportingservices">OsIndependent</OperatingSystem>
    <CountryLocaleId xmlns="http://www.microsoft.com/sql/reportingservices">1033</CountryLocaleId>
    <MoreInformation xmlns="http://www.microsoft.com/sql/reportingservices">

        <Source>Microsoft.ReportingServices.ProcessingCore</Source>
        <Message msrs:ErrorCode="rsProcessingAborted" msrs:HelpLink="http://go.microsoft.com/fwlink/?LinkId=20476&amp;EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&amp;EvtID=rsProcessingAborted&amp;ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&amp;ProdVer=10.50.1600.1" xmlns:msrs="http://www.microsoft.com/sql/reportingservices">An error has occurred during report processing.</Message>
        <MoreInformation>
            <Source>Microsoft.ReportingServices.ProcessingCore</Source>
            <Message msrs:ErrorCode="rsErrorReadingNextDataRow" msrs:HelpLink="http://go.microsoft.com/fwlink/?LinkId=20476&amp;EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&amp;EvtID=rsErrorReadingNextDataRow&amp;ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&amp;ProdVer=10.50.1600.1" xmlns:msrs="http://www.microsoft.com/sql/reportingservices">Cannot read the next data row for the dataset DD_Inventory.</Message>
            <MoreInformation>
                <Source>.Net SqlClient Data Provider</Source>
                <Message>Conversion failed when converting the nvarchar value 'h' to data type int.</Message>
            </MoreInformation>
        </MoreInformation>
    </MoreInformation>
    <Warnings xmlns="http://www.microsoft.com/sql/reportingservices" />
</detail>

How would I parse the following to get the sql message .Net SqlClient Data Provider?

I'm trying to do it with Linq to xml but I'm not having much luck. The xml is recursive so I cant be certain what level the message will appear at

Any ideas?

Thanks

<detail>
    <ErrorCode xmlns="http://www.microsoft.com/sql/reportingservices">rsProcessingAborted</ErrorCode>
    <HttpStatus xmlns="http://www.microsoft.com/sql/reportingservices">400</HttpStatus>
    <Message xmlns="http://www.microsoft.com/sql/reportingservices">An error has occurred during report processing.</Message>
    <HelpLink xmlns="http://www.microsoft.com/sql/reportingservices">http://go.microsoft.com/fwlink/?LinkId=20476&EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&EvtID=rsProcessingAborted&ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&ProdVer=10.50.1600.1</HelpLink>
    <ProductName xmlns="http://www.microsoft.com/sql/reportingservices">Microsoft SQL Server Reporting Services</ProductName>
    <ProductVersion xmlns="http://www.microsoft.com/sql/reportingservices">10.50.1600.1</ProductVersion>
    <ProductLocaleId xmlns="http://www.microsoft.com/sql/reportingservices">1033</ProductLocaleId>
    <OperatingSystem xmlns="http://www.microsoft.com/sql/reportingservices">OsIndependent</OperatingSystem>
    <CountryLocaleId xmlns="http://www.microsoft.com/sql/reportingservices">1033</CountryLocaleId>
    <MoreInformation xmlns="http://www.microsoft.com/sql/reportingservices">

        <Source>Microsoft.ReportingServices.ProcessingCore</Source>
        <Message msrs:ErrorCode="rsProcessingAborted" msrs:HelpLink="http://go.microsoft.com/fwlink/?LinkId=20476&EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&EvtID=rsProcessingAborted&ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&ProdVer=10.50.1600.1" xmlns:msrs="http://www.microsoft.com/sql/reportingservices">An error has occurred during report processing.</Message>
        <MoreInformation>
            <Source>Microsoft.ReportingServices.ProcessingCore</Source>
            <Message msrs:ErrorCode="rsErrorReadingNextDataRow" msrs:HelpLink="http://go.microsoft.com/fwlink/?LinkId=20476&EvtSrc=Microsoft.ReportingServices.Diagnostics.Utilities.ErrorStrings&EvtID=rsErrorReadingNextDataRow&ProdName=Microsoft%20SQL%20Server%20Reporting%20Services&ProdVer=10.50.1600.1" xmlns:msrs="http://www.microsoft.com/sql/reportingservices">Cannot read the next data row for the dataset DD_Inventory.</Message>
            <MoreInformation>
                <Source>.Net SqlClient Data Provider</Source>
                <Message>Conversion failed when converting the nvarchar value 'h' to data type int.</Message>
            </MoreInformation>
        </MoreInformation>
    </MoreInformation>
    <Warnings xmlns="http://www.microsoft.com/sql/reportingservices" />
</detail>

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

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

发布评论

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

评论(1

半山落雨半山空 2024-11-26 04:36:19
var doc = XDocument.Parse(xml);

XNamespace ns = "http://www.microsoft.com/sql/reportingservices";

string message = (from info in doc.Descendants(ns + "MoreInformation")
                  where info.Element(ns + "MoreInformation") == null
                  select (string)info.Element(ns + "Message")).Single();

获取整个文档中名为 MoreInformation 的所有元素,仅获取那些没有嵌套更多 MoreInformation 的元素。对于他们,请选择消息。最后,确保只有一个结果。

var doc = XDocument.Parse(xml);

XNamespace ns = "http://www.microsoft.com/sql/reportingservices";

string message = (from info in doc.Descendants(ns + "MoreInformation")
                  where info.Element(ns + "MoreInformation") == null
                  select (string)info.Element(ns + "Message")).Single();

Take all elements in the whole document with the name MoreInformation, take only those that don't have any more MoreInformation nested inside them. For them, select the message. Finally, make sure there is only one result.

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