这是xml文件的一部分 总的很长 只贴一部分
<?xml version="1.0" encoding="utf-8" ?>
<Message>
<FormName Value="LoginForm">
<ID Value="0001">
<Type>Error</Type>
<Title>test</Title>
<Value>test1</Value>
</ID>
<ID Value="0002">
<Type>Error</Type>
<Title>test</Title>
<Value>test2</Value>
</ID>
</FormName>
<FormName Value="Common">
<ID Value="0001">
<Type>Error</Type>
<Title>test</Title>
<Value>test3</Value>
</ID>
</FormName>
</Message>现在我想在.cs中 ,取得 xml里面 formname的值 和 对应节点下面 的 value的值
请问怎么写!
<?xml version="1.0" encoding="utf-8" ?>
<Message>
<FormName Value="LoginForm">
<ID Value="0001">
<Type>Error</Type>
<Title>test</Title>
<Value>test1</Value>
</ID>
<ID Value="0002">
<Type>Error</Type>
<Title>test</Title>
<Value>test2</Value>
</ID>
</FormName>
<FormName Value="Common">
<ID Value="0001">
<Type>Error</Type>
<Title>test</Title>
<Value>test3</Value>
</ID>
</FormName>
</Message>现在我想在.cs中 ,取得 xml里面 formname的值 和 对应节点下面 的 value的值
请问怎么写!
using System.Xml;void ReadXml(string fileName)
{
XmlDocument xd = new XmlDocument();
xd.Load(fileName);
foreach (XmlNode xn in xd.SelectNodes("Message/FormName"))
{
Console.WriteLine("FormName:" + xn.Attributes["Value"].Value);
foreach (XmlNode childXN in xn.SelectNodes("ID"))
Console.WriteLine("ID:" + childXN.Attributes["Value"].Value);
}
}
<abc>
<Field>100</Field>
<item>
<id>1</id>
<name>zhangsan</name>
<sex>男</sex>
</item>
<item>
<id>2</id>
<name>lisi</name>
<sex>男</sex>
</item>
</abc>
//实体类。
public class Information
{
private string id;
public string Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
public Information()
{
}
public Information(string id,string name,string sex)
{
this.Id = id;
this.Name = name;
this.Sex = sex;
}
}
//读取xml里面的文件信息
List<Information> list = new List<Information>();
//实例化xml
XmlDocument xml = new XmlDocument();
//读取xml文件
xml.Load(@"E:\C#\S2C#\DLCL\打印电脑\MyComputer\XulieHua\XML.xml"); //你的xml地址
string id = "";
string name = "";
string sex = "";
Information info = null;
//////////*******下面开始循环读取xml文件信息********/
///////////////
foreach (XmlNode node in xml.ChildNodes)
{
if (node.Name == "abc")
{
foreach (XmlNode node1 in node.ChildNodes)
{
if (node1.Name == "item")
{
foreach (XmlNode node2 in node1.ChildNodes)
{
switch (node2.Name)
{
case "id":
id = node2.InnerText;
break;
case "name":
name = node2.InnerText;
break;
default:
sex = node2.InnerText;
break;
}
}
info = new Information(id, name, sex);
//将信息保存至集合
list.Add(info);
}
}
}
}
string[] ss = default(string[]);
List<string> ls = new List<string>();
XDocument xd = XDocument.Load(path);
foreach (var item in xd.Root.Elements()) {
if (item.Name == "FormName") {
ls.Add(item.Attribute("Value").Value);
}
}
ss = new string[ls.Count];
ls.CopyTo(ss);
return ss;
}
//
输出
LoginForm
Common
using System;
using System.Xml;
class read
{
static void Main()
{
XmlDocument xd = new XmlDocument();
string filename=@"f:\test\form.xml";
xd.Load(filename);
foreach (XmlNode xn in xd.SelectNodes("Message/FormName"))
{
Console.WriteLine("FormName:" + xn.Attributes["Value"].Value);
foreach (XmlNode childXN in xn.ChildNodes)
{
Console.WriteLine("ID:" + childXN.Attributes["Value"].Value);
Console.WriteLine("Value:" + childXN["Value"].InnerXml);
}
}}
}
{
XmlDocument doc = new XmlDocument(); doc.Load(urlRSS); return doc.GetElementsByTagName("item");
}
public static void Outputxml(string filename)
{
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(fileName); try
{
XmlNodeList NodeList = xmldoc.SelectNodes("Message/FormName");
foreach (XmlNode Node in NodeList)
{ richTextBox1.AppendText( Node["FormName "].Attributes["Value"].Value+"\r\n"); }
}
catch (XmlException ex)
{
}
}