如何使用LINQ to XML来得到这个属性? [英] How to get this attribute using linq to xml?

查看:121
本文介绍了如何使用LINQ to XML来得到这个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的XML:

 <结果ID =1,新的xmlns =htt​​p://schemas.microsoft.com/sharepoint/soap/>
    <错误code取代; 00000000< /错误code取代;
    < ID />
    < Z:排ows_ID =6/>
< /结果>
 

我一直在尝试使用以下方法来获取 ows_ID 值:

 的XNamespace NS =htt​​p://schemas.microsoft.com/sharepoint/soap/;
字符串NEWID =(来自R在resDoc.Descendants(NS +行)
               选择(串)r.Attribute(ows_ID))第一()。
 

不返回任何记录,并且:

 的XNamespace NS =htt​​p://schemas.microsoft.com/sharepoint/soap/;
字符串NEWID =(来自R在resDoc.Descendants(NS +Z:排)
               选择(串)r.Attribute(ows_ID))第一()。
 

这将引发从一个错误的

什么是正确的方式,我得到这个值?

更新 - 完成 Z:行节点

 < Z:排ows_ContentTypeId =0x010090ADDB8ED990B741A07020AB204CDB880100311975766C6F0E4CBE4EBFBC3CBFD9ABows_Title =测试2 attachments343434ows_AggregateDesc =&放大器; LT; D​​IV class="ExternalClass05363FABD7BB400483A6AE4BB3B9B6CE"><p>yes?</p></div>" ows_Remarks =&放大器; LT; D​​IV class="ExternalClassB63AA0BFC1804E24B10C9559D7FBEBA5"><p>no?</p></div>" ows_PublishDate =2012-06-15 12:00:00ows_MemoStatus =提交ows_ID =6ows_ContentType =FridayMemoows_Modified =2012-06-27 14时00分47秒ows_Created =2012-06- 27十四时00分47秒ows_Author =49; #Abe Miesslerows_Editor =49; #Abe Miesslerows_owshiddenversion =1ows_WorkflowVersion =1ows__UIVersion =512ows__UIVersionString =1.0ows_Attachments =0 ows__ModerationStatus =0ows_LinkTitleNoMenu =试验2 attachments343434ows_LinkTitle =试验2 attachments343434ows_LinkTitle2 =试验2 attachments343434ows_SelectTitle =6ows_Order =600.000000000000ows_GUID ={393F36F5-FFA8-4F6E-A12A-1107AA713F25} ows_FileRef =6;#NC / CEO /列表/ FridayMemo / 6_.000ows_FileDirRef =6;#NC / CEO /列表/ FridayMemoows_Last_x0020_Modified =; 6#2012-06-27 14时00分47秒 ows_Created_x0020_Date =; 6#2012-06-27十四点00分47秒ows_FSObjType =6,#0ows_SortBehavior =6,#0ows_PermMask =0x7fffffffffffffffows_FileLeafRef =; 6#6_.000ows_UniqueId = 6,#{} F4C6B345-4590-4791-9384-18983132F055ows_ProgId =6;#ows_ScopeId =6;#{8450C4BD-0866-40ED-A0CD-22E3105E0845}ows__EditMenuTableStart =6_.000ows__EditMenuTableStart2 = 6ows__EditMenuTableEnd =6ows_LinkFilenameNoMenu =6_.000ows_LinkFilename =6_.000ows_LinkFilename2 =6_.000ows_ServerUrl =/ NC / CEO /列表/ FridayMemo / 6_.000ows_En codedAbsUrl =htt​​p://sptestmnc.nevcounty.net/nc/ceo/Lists/FridayMemo/6_.000ows_BaseName =6_ows_MetaInfo =6;#ows__Level =1ows__IsCurrentVersion =1ows_ItemChildCount =6 ;#0ows_FolderChildCount =6,#0
  的xmlns:Z =#RowsetSchema/>
 

解决方案

  VAR NS = XNamespace.Get(#RowsetSchema);
    VAR ID = XML
      .Descendants(NS +行)
      。选择(行=> row.Attribute(ows_ID)值。)
      。第一();
 

  VAR NS = XNamespace.Get(#RowsetSchema);
    VAR ID = XML
      .Descendants(NS +行)
      。第一()
      属性约(ows_ID)
      。值;
 

I have the following XML:

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

I have been trying to get the ows_ID value using the following methods:

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

which returns no records, and:

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

which throws an error from the :

What is the proper way for me to get this value?

UPDATE - Complete z:row node

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

or

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

这篇关于如何使用LINQ to XML来得到这个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆