如何在c#中检索xml属性值 [英] how to retrieve xml attribute value in c#

查看:81
本文介绍了如何在c#中检索xml属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <  菜单 >  

< RootMenu id = STE name = 股票查询 >

< SubMenu id = ITM 名称 = 项目查询 form = ItemEnquiryForm / >
< SubMenu id = LOC 名称 = 位置查询 表格 = LocationEnquiryForm / >
< SubMenu id = LOT name = 批量查询 表格 = LotEnquiryForm / >
< SubMenu id = PKG name = 包裹查询 表格 = PackageEnquiryForm / >
< / RootMenu >

< RootMenu id = DIS name = 发货 >
< span class =code-keyword>< SubMenu id = PPK name = Piak和Pack 表格 = PickAndPackForm / >
< SubMenu id = PUT name = 2步上架 form = PutAwayForm / >

< span class =code-keyword>< / RootMenu >

< /菜单 >





所有



谁能告诉我怎么样我可以检索RootMenu的id = STE的subMenu名称值。我想得到一个列表< string>对于所有子菜单的名称值属于RootMenu,id = STE。



谢谢

解决方案

你也可以使用linkQ到XML。以下是供参考的代码

 XElement xelement = XElement.Load(@C:\ Menen.xml); 
IEnumerable< XElement> menus = xelement.Elements();
List< string> subMenuList = new List< string>();
foreach(菜单中的var菜单)
{
if(menu.Attribute(id)。Value ==STE)
{
foreach(var menu.Elements())中的子菜单
{
subMenuList.Add(submenu.Attribute(name)。Value);
}
}
}


你也可以在Xml DOM中使用XPath,如下所示: br />

  string  name; 
XmlDocument xml = new XmlDocument();
xml.Load( theFile.xml);
// 或者在XmlDocument中加载xml数据的任何其他方法。
// 例如,如果您的xml数据是字符串,请使用LoadXml方法。
XmlElement elt = xml.SelectSingleNode( // SubMenu [@ id ='STE'] as XmlElement;
if (elt!= null
{
name = elt.GetAttribute( name);
}





XPath / XQuery语法可以在互联网上找到: XPath语法(参见选择节点章节)



如果你需要检索一个元素列表,你可以使用SelectNodes方法(它返回一个XmlNodeList对象)。



 XmlNodeList子菜单= xml.SelectNodes(  // RootMenu [@ id ='STE'] / SubMenu); 
List< string> names = new List< string>();
foreach (XmlNode n 子菜单中)
{
if (n XmlElement)
names.Add((n as XmlElement)。GetAttribute( name));
}


在.Net中有许多管理XML的工具。



XmlDocument

XmlReader



以下使用 XmlReader 查找特定元素并获取属性值。



< pre lang =c#> string myXml = < ;试验>< AI = \ 1\/>< AI = \ 2\/>< / A>< / A>< /试验>;
System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myXml));
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(ms);

int firstIValue = 0 ;

while (xr.Read())
{
if (xr.NodeType == System.Xml.XmlNodeType.Element)
if (xr.Name == a
{
firstIValue = Convert.ToInt32(xr [ i]);
break ;
}
}


<Menus>

 <RootMenu id="STE" name="Stock Enquiry">

  <SubMenu id="ITM" name="Item Enquiry" form="ItemEnquiryForm" />
  <SubMenu id="LOC" name="Location Enquiry" form="LocationEnquiryForm" />
  <SubMenu id="LOT" name="Lot Enquiry" form="LotEnquiryForm" />
  <SubMenu id="PKG" name="Package Enquiry" form="PackageEnquiryForm" />
 </RootMenu>

 <RootMenu id="DIS" name="Dispatch">
  <SubMenu id="PPK" name="Piak and Pack" form="PickAndPackForm" />
   <SubMenu id="PUT" name="2 Steps Putaway" form="PutAwayForm" />

 </RootMenu>

  </Menus>



hi,all

can anyone tell me how can i retrieve the subMenu's name values from which the RootMenu's id=STE. i would like to get a list<string> for all the submenu's name values which belong to RootMenu with id=STE.

thanks

解决方案

You can also use linkQ to XML. Below is code for reference

XElement xelement = XElement.Load(@"C:\Menu.xml");
           IEnumerable<XElement> menus = xelement.Elements();
           List<string> subMenuList = new List<string>();
           foreach (var menu in menus)
           {
               if (menu.Attribute("id").Value == "STE")
               {
                   foreach (var submenu in menu.Elements())
                   {
                       subMenuList.Add(submenu.Attribute("name").Value);
                   }
               }
           }


You can also use XPath within the Xml DOM like this :

string name;
XmlDocument xml = new XmlDocument();
xml.Load("theFile.xml"); 
// Or any other method to load your xml data in the XmlDocument.
// For example if your xml data are in a string, use the LoadXml method.
XmlElement elt = xml.SelectSingleNode("//SubMenu[@id='STE']") as XmlElement;
if(elt!=null)
{
  name=elt.GetAttribute("name");  
}



XPath/XQuery syntax can be found on internet: XPath Syntax (See the Selecting Nodes chapter)

If you need to retrieve a list of elements, you can use the SelectNodes method (it returns an XmlNodeList object).

XmlNodeList submenus = xml.SelectNodes("//RootMenu[@id='STE']/SubMenu");
List<string> names = new List<string>();
foreach(XmlNode n in submenus)
{
  if(n is XmlElement)
    names.Add((n as XmlElement).GetAttribute("name"));
}


There are a number of tools for managing XML in .Net.

XmlDocument
XmlReader

The following uses an XmlReader to find a specific element and get an attribute value.

string myXml = "<test><a i=\"1\"/><a i=\"2\"/></a></a></test>";
System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myXml));
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(ms);

int firstIValue = 0;

while (xr.Read())
{
  if(xr.NodeType == System.Xml.XmlNodeType.Element)
    if (xr.Name == "a")
    {
      firstIValue = Convert.ToInt32(xr["i"]);
      break;
    }
}


这篇关于如何在c#中检索xml属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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