我正在尝试读取xml的特定节点值:
<dsobject handle="Document-27897" classname="Document" baseClassname="Document">
<props>
<prop name="summary" type="10">2952239651</prop>
<prop name="highest_version_used" type="1">1</prop>
<prop name="routing_template_id" type="1" />
<prop name="modified_date" type="4">Thu Jul 21 15:02:58 EDT 2011</prop>
<prop name="locale" type="10">en</prop>
<prop name="max_versions" type="1">4</prop>
<prop name="canAddVersions" type="3">true</prop>
<prop name="author" type="10"></prop>
<prop name="title" type="10">806</prop>
<prop name="create_date" type="4">Thu Jul 21 15:02:58 EDT 2011</prop>
<prop name="description" type="11">SELF HELP (KVII) ASSOC-CITY CAPITAL</prop>
<prop name="original_file_name" type="10">DOC20110721150230-853.pdf</prop>
<prop name="webdav_title" type="10"></prop>
<prop name="keywords" type="10"></prop>
<prop name="add_as_draft" type="3">false</prop>
<prop name="cfCopyID" type="10"></prop>
<prop name="lastSynchronized" type="4" />
<prop name="isPlaceholder" type="3">false</prop>
<prop name="isRecord" type="3">false</prop>
<prop name="rm_instance_handle" type="10" />
<prop name="client_data" type="10"></prop>
<prop name="copiedFrom" type="10"></prop>
<prop name="cfSpecialWords" type="10"></prop>
<prop name="autoMovePreferredVersion" type="3">true</prop>
<prop name="size" type="1">1365070</prop>
<prop name="isInteract" type="3">false</prop>
<prop name="expiration_date" type="4" />
<prop name="readyForDeclare" type="3">false</prop>
</props>
我需要获得特定元素的价值:
<prop name="summary" type="10">2952239651</prop>
<prop name="description" type="11">SELF HELP (KVII) ASSOC-CITY CAPITAL</prop`>
<prop name="original_file_name" type="10">DOC20110721150230-853.pdf</prop>
这是我用来获取元素值的代码,其属性名称为&#34; summary&#34;,但我无法使用相同的查询来获取其他值(====&gt; ; name =&#34; description,name =&#34; original_file_name)
var number = (from x in collection.Root.Descendants("dsobject") where x.Attribute("handle").Value.Contains("Document") select x.Element("props")).ToList();
var vals = (from c in number where c.Element("prop").Attribute("name").Value == "summary" select c.Element("prop").Value).ToList();
有人能引导我朝着正确的方向前进吗?感谢
答案 0 :(得分:0)
如果您知道元素存在,那么执行
string summary = xdoc.Root.Element("props").Elements("prop").First(e => (string)e.Attribute("name") == "summary").Value;
string description = xdoc.Root.Element("prop").Elements("prop").First(e => (string)e.Attribute("name") = "description").Value;
等等。
如果有多个props
个元素,并且您需要一个例如summary
值然后使用
List<string> summaries = xdoc.Root.Elements("props").Elements("prop").Where(e => (string)e.Attribute("name") == "summary").Select(s => s.Value).ToList();
答案 1 :(得分:0)
使用System.Xml.Linq
,您可以执行以下操作。
var xmlFile = XDocument.Load("xmlpath");
var propElements = xmlFile.Descendants("prop");
var summaryProp = propElements.Where(e =>
string.Compare(e.Attribute("name").Value, "summary"
, StringComparison.CurrentCultureIgnoreCase) == 0)
.FirstOrDefault();
var summaryValue = summaryProp.Value;
变量summaryValue将保存值2952239651。
此方法假定只有一个元素将命名为summary。
更新
抱歉 - 以下代码可用于检索多个道具的摘要:
var xmlFile = XDocument.Load("xmlpath");
var propElements = xmlFile.Descendants("prop");
var summaryProp = propElements.Where(e =>
string.Compare(e.Attribute("name").Value, "summary"
, StringComparison.CurrentCultureIgnoreCase) == 0)
.Select(prop => prop.Value).ToList();