winform怎么解析这段XML
1<?xml version="1.0" encoding="gb2312" ?>
2<function>
3<function_id>infolist</function_id>
4<out_code>0</out_code>
5<out_text>操作成功</out_text>
6<rows>1802</rows>
7<DrugInfoList>
8<row>
9<classCode>011405</classCode>
10<drugType>1</drugType>
11<drugCode>01140509</drugCode>
12<drugCode2></drugCode2>
13<useLevel>2</useLevel>
14<drugName>硫普罗宁</drugName>
15 。。。
16</row>
17<row>
18<classCode>011405</classCode>
19<drugType>1</drugType>
20<drugCode>01140510</drugCode>
21<drugCode2></drugCode2>
22<useLevel>2</useLevel>
23<drugName>水飞蓟宾</drugName>
24 。。。
25</row>
26</DrugInfoList>
27</function>
请问怎么解析这段XML
谢谢
[解决办法]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string xml = @"
<function>
<function_id>infolist</function_id>
<out_code>0</out_code>
<out_text>操作成功</out_text>
<rows>1802</rows>
<DrugInfoList>
<row>
<classCode>011405</classCode>
<drugType>1</drugType>
<drugCode>01140509</drugCode>
<drugCode2></drugCode2>
<useLevel>2</useLevel>
<drugName>硫普罗宁</drugName>
</row>
<row>
<classCode>011405</classCode>
<drugType>1</drugType>
<drugCode>01140510</drugCode>
<drugCode2></drugCode2>
<useLevel>2</useLevel>
<drugName>水飞蓟宾</drugName>
</row>
</DrugInfoList>
</function>
";
XElement xe = XElement.Parse(xml);
var out_code = xe.Element("out_code").Value;
var out_text = xe.Element("out_text").Value;
var drugNames = xe.Descendants("drugName").Select(x => x.Value);
MessageBox.Show(out_code);
MessageBox.Show(out_text);
foreach (var i in drugNames)
{
MessageBox.Show(i);
}
}
}
}
string xml = @"
<function>
<function_id>infolist</function_id>
<out_code>0</out_code>
<out_text>操作成功</out_text>
<rows>1802</rows>
<DrugInfoList>
<row>
<classCode>011405</classCode>
<drugType>1</drugType>
<drugCode>01140509</drugCode>
<drugCode2></drugCode2>
<useLevel>2</useLevel>
<drugName>硫普罗宁</drugName>
</row>
<row>
<classCode>011405</classCode>
<drugType>1</drugType>
<drugCode>01140510</drugCode>
<drugCode2></drugCode2>
<useLevel>2</useLevel>
<drugName>水飞蓟宾</drugName>
</row>
</DrugInfoList>
</function>
";
XElement xe = XElement.Parse(xml);
//获取单个字节点的值:
string functionId = xe.Element("function_id").Value;
Console.WriteLine(functionId);
//遍历DrugInfoList节点下的所有子节点的值
IEnumerable<XElement> ie= xe.Elements("DrugInfoList").Elements();
foreach (XElement x in ie)
{
Console.WriteLine("NodeName="+x.Name);
foreach (XElement e in x.Elements())
Console.WriteLine("Name:"+e.Name+",Value:"+e.Value);
}
[XmlRoot(ElementName = "function")]
public class FunctionInfo
{
public string function_id { get; set; }
public string out_code { get; set; }
public string out_text { get; set; }
public string rows { get; set; }
[XmlArray(ElementName = "DrugInfoList")]
[XmlArrayItem(ElementName = "row", Type=typeof(DrugInfo))]
public List<DrugInfo> drugInfoList { get; set; }
}
public class DrugInfo
{
public string classCode { get; set; }
public string drugType { get; set; }
public string drugCode { get; set; }
public string drugCode2 { get; set; }
public string useLevel { get; set; }
public string drugName { get; set; }
}
--------------------------------------------
FunctionInfo funcInfo = null;
using (var fs = File.OpenRead("XmlFile1.xml"))
{
var xmlSer = new XmlSerializer(typeof(FunctionInfo));
funcInfo = (FunctionInfo)xmlSer.Deserialize(fs);
}