.net读取XML属性,根据属性显示该节点的值
XML code
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Menu>
<Item Title="a1"><![CDATA[a11]]></Item>
<Item Title="a2"><![CDATA[a22]]></Item>
<Item Title="a3"><![CDATA[a33]]></Item>
<Item Title="a4"><![CDATA[a44]]></Item>
<Item Title="a5"><![CDATA[a55]]></Item>
</Menu>
我想根据Title来显示该节点的值。
如我查询到Title=“a1”,然后显示a11在界面上
求参考代码!!!
------解决方案--------------------http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx
------解决方案--------------------http://www.knowsky.com/340889.html
------解决方案--------------------
public string Test
{
//定义两个属性string ItemValue, ItemProperty
}
public List<Test> GetList()
{
List<Test> list = new List<Test>();
XmlReader reader = XmlReader.Create(xmlFile);
reader.ReadToFollowing("Item");
do
{
Test test = new Test();
test.ItemProperty = reader.GetAttribute("Title");
test.ItemValuee = reader.ReadString();
list.Add(test);
}
while (reader.ReadToNextSibling("Title"));
reader.Close();
return list;
}
把XML文件读到List中,查询
public string Find(string title)
{
List<Test> list = GetList();
Test test = list.Find(p => p.ItemProperty == title);
return test.ItemValue;
}
------解决方案--------------------
private void ShowValue(string hehe)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xml文件位置);
XmlNode xnode = xdoc.DocumentElement;
foreach (XmlNode node in xnode.ChildNodes)
{
if (node.Attributes[0].Value == hehe)
{
MessageBox.Show(node.InnerText);
}
}
}
------解决方案--------------------
XmlDocument doc = new XmlDocument();
doc.Load(你的文件);
XmlNode node = doc.SelectSingleNode("/Menu/Item[@Title='a1']");