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谢谢
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.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);
}
}
}
}
一、引入命名空间 System.xml;
二、实例化XmlDocument对象
三、利用XmlDocument对象的Load()方法将指定的XML文件加载入XmlDocument对象
四、取得根节点对象
五、遍历其子节点
示例using System.XMl;
Class Program
{
static void Main()
{
XmlDocument myxml = new XmlDocument();
myxml.Load("student.xml");
XmlNode rootNode = myxml.DocumentElement;
foreach(XmlNode detailNode in rootNode.ChildNodes)
{
//执行操作
}
}
}
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);
}
<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);val element=from el in xe.Elements("DrugInfoList").Elements()
select el;
element.ToList<XElement>().ForEach(x =>
{Console.WriteLine("NodeName="+x.Name);
if(x.HasElements)
{
x.ToList<XElement>().ForEach(xc =>
{
Console.WriteLine("Name:"+xc.Name+",Value:"+xc.Value);
}
}}
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);
}
如果需要修改或新建,建议使用xmldocment对象,比较方便操作。
string xml = @"
<EP>
<q>
<qc>1</qc>
<no>1</no>
<fno>part 1</fno>
<sno>section A</sno>
<qb>Are you ready? A ok B not ok</qb>
<qa></qa>
</q>
<q>
<qc>1</qc>
<no>1</no>
<fno>part 2</fno>
<sno>section B</sno>
<qb>Are you ready? A ok B not ok</qb>
<qa></qa>
</q>
<q>
<qc>1</qc>
<no>1</no>
<fno>part 3</fno>
<sno>section C</sno>
<qb>Are you ready? A ok B not ok</qb>
<qa></qa>
</q>
</EP>
";
XElement xe = XElement.Parse(xml);
var fno = xe.Element("fno").Value;
var sno = xe.Element("sno").Value;
var qbs = xe.Descendants("qb").Select(x => x.Value);
MessageBox.Show(fno);
MessageBox.Show(sno);
foreach (var i in qbs)
{
MessageBox.Show(i);
}