C# 读取xml中特定节点的属性值 xmldocument方式

2007-06-08 17:55

下面这个xml 文件,要找的路径是"WTTData"Component"ComponentData"ComponentData"ObjectQueryBuilder 找属性field为datastore的expression节点的value值.

<WTTData>
     <Component SetName="Explorers" GroupName="JobExplorer">
       <ComponentData Name="JobExplorer" Version="2.2.2098.0">
         <ComponentData Name="HorzSplitter" Version="2.2.2098.0">
           <Data Position="185" />
         </ComponentData>
         <ComponentData Name="VertSplitter" Version="2.2.2098.0">
           <Data Position="150" />
         </ComponentData>
         <ComponentData Name="TabbedObjectTreeControl" Version="2.2.2098.0" Visible="True">
           <Data SelectedTreeNodePath="$" CurrentTab="0" />
         </ComponentData>
         <ComponentData Name="ConfigurableListControl" Version="2.2.2098.0">
           <Column Name="" Width="32" SortIndex="0" SortOrder="None" ColumnOrder="0" Default="True" />
           <Column Name="Id" Width="70" SortIndex="-1" SortOrder="None" ColumnOrder="1" Default="True" />
           <Column Name="Name" Width="352" SortIndex="-1" SortOrder="None" ColumnOrder="2" Default="True" />
           <Column Name="AssignedToAlias" Width="80" SortIndex="-1" SortOrder="None" ColumnOrder="3" Default="True" />
           <Column Name="FullPath" Width="190" SortIndex="-1" SortOrder="None" ColumnOrder="4" Default="True" />
           <Column Name="LastUpdatedTime" Width="150" SortIndex="1" SortOrder="Descending" ColumnOrder="5" Default="True" />
         </ComponentData>
         <ComponentData Name="QueryBuilder" Version="2.2.2098.0" Visible="True">
           <ObjectQueryBuilder ControlType="Microsoft.DistributedAutomation.UI.QueryBuilder" ControlAssembly="Microsoft.WTT.UI.Controls.ObjectControls" Assembly="WTTOMJobs" Type="Microsoft.DistributedAutomation.Jobs.Job" Title="Job Query">
             <Expression Index="1" Static="True" AttachWith="" Mapping="DataStore" Field="DataStore" Operator="Equals" Value="WTTMobile1" />
             <Expression AttachWith="And" Mapping="Id" Field="ID" Operator="Greater" Value="733454" />
             <Expression AttachWith="And" Mapping="Id" Field="ID" Operator="Less" Value="733740" />
           </ObjectQueryBuilder>
         </ComponentData>
         <ComponentData Name="ToolBar" Version="2.2.2098.0">
           <Data DefinitionController="WTTMobile1" HideQueryBuilder="True" HideHierarchy="True" MaxItemsToDisplay="500" />
         </ComponentData>
       </ComponentData>
     </Component>
     <ActiveComponent SetName="Explorers" GroupName="JobExplorer" />
</WTTData>

实现代码是:

{

{

xDoc.Load(filename);

xElem1 = (

{

dataStoreString = xElem1.GetAttribute(

}

}

}

1,定义一个XmlDocument对象xDoc

2,通过XmlDocument来load需要读取的xml文件

3,通过XmlDocument的SelectSingleNode来找到节点,并把节点转换为XmlElement

4,XmlElement 可以对节点的属性进行操作

注意两点:

1,

其中xDoc.SelectSingleNode("descendant::Expression[@Field='DataStore']");

SelectSingleNode参数里带有过滤条件,匹配公式是

descendant::nodename[@attribute='attributevalue']

2,xElem1 = (XmlElement)xDoc.SelectSingleNode("descendant::Expression[@Field='DataStore']");把XmlNode强制转为XmlElement,理由是XmlElement是XmlNode的子类,前者比后者提供更多好用的方法,比如属性的读和写.XmlNode除了可以转为XmlAttribute.

staticpublicstring getDataStore(string filename)string dataStoreString = string.Empty;if (!string.IsNullOrEmpty(filename))XmlDocument xDoc = newXmlDocument();XmlElement xElem1;XmlElement)xDoc.SelectSingleNode("descendant::Expression[@Field='DataStore']");if (xElem1 != null)"Value");return dataStoreString;

posted on 2007-09-03 11:35  步走高飞  阅读(5683)  评论(0编辑  收藏  举报

导航