如何使用的SelectSingleNode获得属性值? [英] How to get attribute value using SelectSingleNode?

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

问题描述

我解析XML文档,我需要找出GID(属性)值(3810)。



根据的SelectSingleNode()。我发现它是不容易找到的属性名称和它的价值。



我可以用这个方法还是我必须切换到另一种方式。



附件是我的代码。



如何使用 3810 >为 GID 。谢谢你。



我的test.xml文件,如下



<预类=郎咸平的XML prettyprint-覆盖> <?XML版本=1.0>?;
<根和GT;
< VERSIONINFO日期=2007-11-28版本=1.0.0.2/>
<属性>
将; AttrDir名=EFEMDIRID =1>
< AttrDir NAME =定位仪DIRID =2>
< AttrDir NAME =SequenceIDDIRID =3>
< AttrObj文本=Slot01GID =3810单位=规模=1/>
< AttrObjCount值=1/>
< / AttrDir>
< / AttrDir>
< / AttrDir>
< /属性>
< /根>



我写的test.cs中,如下



<前类=郎-CS prettyprint-覆盖> 公共类样品
{
公共静态无效的主要()
{
XmlDocument的DOC =新的XmlDocument ();
doc.Load(的test.xml);

XmlNode的书;
XmlNode的根= doc.DocumentElement;

=书root.SelectSingleNode(属性[AttrDir [@名称='EFEM'] / AttrDir [@名称='定位仪'] / AttrDir [@名称='SequenceID'] / AttrObj [@文本='Slot01']]);

Console.WriteLine(显示修改的XML文档......);
doc.Save(Console.Out);
}
}

[更新06/10/2010]




  1. XML文件是一个复杂的文件。包括数千导报的。但对于每个XPath时,GID是独一无二的。


  2. 我将XML文件加载到TreeView控件。 this.treeView1.AfterSelect + =新System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect); 。当 treeView1_AfterSelect 事件发生后, e.Node.FullPath 将返回一个字符串值。


  3. 我解析字符串值 e.Node.FullPath 。然后,我得到的XPath以上的成员。然后我试图找到其中的GID项被选定。




我需要找到GID值确实是一个返回值。

解决方案

您可以写

  XmlAttribute gidAttribute =(XmlAttribute)book.Attributes .GetNamedItem(GID); 
字符串gidValue = NULL;
如果(gidAttribute!= NULL)
值= gidAttribute.Value;



另外,展开XPath来获取属性,如:

 属性[AttrDir [@名称='EFEM'] / AttrDir [@名称='定位仪'] / AttrDir [@名称='SequenceID'] / AttrObj [@Text ='Slot01'] / @ GID 

如果该@gid是唯一的,那么你可以简单地使用中的XPath

 // AttrObj [@ GID ='3810']

要获取给定ID所需的节点。但是请注意,每个请求将通过整个文档进行搜索。这将是更有效的获取所有的节点,然后把它们放到一个地图,通过ID关键字。

 // AttrObj [@gid]

使用 XmlNode.SelectNodes 来获取与@gid属性的所有AttrObj的列表。


I am parsing a xml document, I need find out the gid (an attribute) value (3810).

Based on SelectSingleNode(). I found it is not easy to find the attribute name and it's value.

Can I use this method or must I switch to another way.

Attached is my code.

How can I use book obj to get the attribute value3810 for gid. Thank you.

My test.xml file as below

<?xml version="1.0" ?>
<root>
   <VersionInfo date="2007-11-28" version="1.0.0.2" />
   <Attributes>
      <AttrDir name="EFEM" DirID="1">
         <AttrDir name="Aligner" DirID="2">
            <AttrDir name="SequenceID" DirID="3">
               <AttrObj text="Slot01" gid="3810" unit="" scale="1" />
               <AttrObjCount value="1" />
           </AttrDir>
         </AttrDir>
      </AttrDir>
   </Attributes>
</root>

I wrote the test.cs as below

public class Sample
{    
    public static void Main()
    {    
        XmlDocument doc = new XmlDocument();
        doc.Load("test.xml");

        XmlNode book;
        XmlNode root = doc.DocumentElement;

        book = root.SelectSingleNode("Attributes[AttrDir[@name='EFEM']/AttrDir[@name='Aligner']/AttrDir[@name='SequenceID']/AttrObj[@text='Slot01']]");

        Console.WriteLine("Display the modified XML document....");
        doc.Save(Console.Out);
    }
}

[Update 06/10/2010]

  1. The xml file is a complex file. Included thousands of gids. But for each of Xpath, the gid is unique.

  2. I load the xml file to a TreeView control. this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);. When treeView1_AfterSelect event occurred, the e.Node.FullPath will return as a String Value.

  3. I parse the string Value e.Node.FullPath. Then I got the member of XPath Above. Then I tried to find which gid item was selected.

I need find the gid value as a return value indeed.

解决方案

You can write

XmlAttribute gidAttribute = (XmlAttribute)book.Attributes.GetNamedItem("gid");
String gidValue = null;
if (gidAttribute!=null)
    value = gidAttribute.Value;

Alternatively, expand the Xpath to fetch the attribute, e.g.

Attributes[AttrDir[@name='EFEM']/AttrDir[@name='Aligner']/AttrDir[@name='SequenceID']/AttrObj[@text='Slot01']]/@gid

If the @gid is unique, then you can simply use the Xpath

"//AttrObj[@gid='3810']"

To fetch the desired node with the given id. But note that each request will search through the entire document. It will be more efficient to fetch all the nodes, and then put them in a map, keyed by id.

"//AttrObj[@gid]"

Use XmlNode.SelectNodes to fetch a list of all AttrObj with a @gid attribute.

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

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