C # xmldocument Method for reading the attribute values of specific nodes in XML

Source: Internet
Author: User

The following XML
File. The path is "wttdata" component "componentdata" componentdata "objectquerybuilder. Find the value of the expression node whose attribute field is datastore.

<Wttdata>
<Component setname = "schedulers"
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"
Controlassepolicy = "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 = "schedulers" groupname = "jobexplorer"/>
</Wttdata>

The implementation code is:

{

{

Xdoc. Load (filename );

Xelem1 = (

{

Datastorestring = xelem1.getattribute (

}

}

}

1. Define an xmldocument object xdoc

2. Use xmldocument to load the XML file to be read

3. Find the node through selectsinglenode of xmldocument and convert the node to xmlelement.

4. xmlelement can operate on node attributes.

Note:

1,

Xdoc. selectsinglenode ("Descendant: expression [@ field = 'datastore']");

The selectsinglenode parameter contains a filter condition. The matching formula is

Descendant: nodename [@ attribute = 'bubutevalue']

2, xelem1 = (xmlelement) xdoc. selectsinglenode ("Descendant: expression [@ field = 'datastore']"); forces the conversion of xmlnode to xmlelement on the grounds that xmlelement is a subclass of xmlnode, the former provides more useful methods than the latter, such as reading and writing attributes. xmlnode can be converted into 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.