簡體   English   中英

如何使用linq到xml獲取此屬性?

[英]How to get this attribute using linq to xml?

我有以下XML:

<Result ID="1,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <ErrorCode>0x00000000</ErrorCode>
    <ID />
    <z:row ows_ID="6" />
</Result>

我一直在嘗試使用以下方法獲取ows_ID值:

XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/";
string newId = (from r in resDoc.Descendants(ns + "row")
               select (string)r.Attribute("ows_ID")).First();

它不返回任何記錄,並且:

XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/";
string newId = (from r in resDoc.Descendants(ns + "z:row")
               select (string)r.Attribute("ows_ID")).First();

從以下引發錯誤:

獲得這個價值的正確方法是什么?

更新 - 完成z:row節點

<z:row ows_ContentTypeId="0x010090ADDB8ED990B741A07020AB204CDB880100311975766C6F0E4CBE4EBFBC3CBFD9AB" ows_Title="test 2 attachments343434" ows_AggregateDesc="&lt;div class=&quot;ExternalClass05363FABD7BB400483A6AE4BB3B9B6CE&quot;&gt;&lt;p&gt;yes?&lt;/p&gt;&lt;/div&gt;" ows_Remarks="&lt;div class=&quot;ExternalClassB63AA0BFC1804E24B10C9559D7FBEBA5&quot;&gt;&lt;p&gt;no?&lt;/p&gt;&lt;/div&gt;" ows_PublishDate="2012-06-15 12:00:00" ows_MemoStatus="Submitted" ows_ID="6" ows_ContentType="FridayMemo" ows_Modified="2012-06-27 14:00:47" ows_Created="2012-06-27 14:00:47" ows_Author="49;#Abe Miessler" ows_Editor="49;#Abe Miessler" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="test 2 attachments343434" ows_LinkTitle="test 2 attachments343434" ows_LinkTitle2="test 2 attachments343434" ows_SelectTitle="6" ows_Order="600.000000000000" ows_GUID="{393F36F5-FFA8-4F6E-A12A-1107AA713F25}" ows_FileRef="6;#nc/ceo/Lists/FridayMemo/6_.000" ows_FileDirRef="6;#nc/ceo/Lists/FridayMemo" ows_Last_x0020_Modified="6;#2012-06-27 14:00:47" ows_Created_x0020_Date="6;#2012-06-27 14:00:47" ows_FSObjType="6;#0" ows_SortBehavior="6;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="6;#6_.000" ows_UniqueId="6;#{F4C6B345-4590-4791-9384-18983132F055}" ows_ProgId="6;#" ows_ScopeId="6;#{8450C4BD-0866-40ED-A0CD-22E3105E0845}" ows__EditMenuTableStart="6_.000" ows__EditMenuTableStart2="6" ows__EditMenuTableEnd="6" ows_LinkFilenameNoMenu="6_.000" ows_LinkFilename="6_.000" ows_LinkFilename2="6_.000" ows_ServerUrl="/nc/ceo/Lists/FridayMemo/6_.000" ows_EncodedAbsUrl="http://sptestmnc.nevcounty.net/nc/ceo/Lists/FridayMemo/6_.000" ows_BaseName="6_" ows_MetaInfo="6;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="6;#0" ows_FolderChildCount="6;#0" 
  xmlns:z="#RowsetSchema" />
    var ns = XNamespace.Get("#RowsetSchema");
    var id = xml
      .Descendants(ns + "row")
      .Select(row => row.Attribute("ows_ID").Value)
      .First();

要么

    var ns = XNamespace.Get("#RowsetSchema");
    var id = xml
      .Descendants(ns + "row")
      .First()
      .Attribute("ows_ID")
      .Value;

您沒有為Descendants方法正確指定名稱空間。 它需要一個XName (它提供一個來自字符串的隱式轉換,欺騙你認為它需要一個字符串)。 您可以使用XNameGet(string,string)靜態方法來指定命名空間:

string ns = "http://schemas.microsoft.com/sharepoint/soap/";
string newId = (from r in resDoc.Descendants(XName.Get("row",ns))
    select (string)r.Attribute("ows_ID")).First();

根據OP的請求,這里是我編寫的完整示例代碼,它在LINQPad中編譯並正常工作:

var ns = "http://schemas.microsoft.com/sharepoint/soap/";
var xml = 
    @"<Result ID=""1,New"" xmlns=""" + ns + @""">" +
        @"<ErrorCode>0x00000000</ErrorCode>" +
        @"<ID />" +
        @"<z:row ows_ID=""6"" />" +
    @"</Result>";

XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
mgr.AddNamespace("z", "http://schemas.microsoft.com/sharepoint/soap/");
XmlParserContext ctx = new XmlParserContext(null, mgr, null, XmlSpace.Default);
XDocument resDoc;
using( XmlReader reader = XmlReader.Create(new StringReader(xml), null, ctx) ) {
    resDoc = XDocument.Load(reader);
}

string newId = (from r in resDoc.Descendants(XName.Get("row",ns))
    select (string)r.Attribute("ows_ID")).First();

Console.WriteLine( newId );     // prints "6"

您必須使用'z'命名空間,該命名空間必須在此代碼中未顯示的位置定義才能訪問該元素,您正在使用的那個是您為根元素設置的那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM