2011-07-08 126 views
0

我將如何解析以下內容以獲取sql消息.Net SqlClient數據提供程序?Linq到Xml搜索XML而不知道確切的結構

我試圖用Linq來做到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> 

回答

1
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嵌套它們。對於他們來說,選擇消息。最後,確保只有一個結果。

+0

你是絕對的男人!謝謝! :) – Mantisimo