日期:2014-05-18  浏览次数:21150 次

如何解析这样的xml高手站出来帮下
<?xml version="1.0" encoding="utf-8"?>
<Config>
<Step StepName="Reg">
<Reg name="" Regpath="" values="">
</Step>
<Step StepName="FileMove">
<FileMove name="" path="">
<FileMove name="" path="">
</Step>

</Config>


我的大概这样的怎么解析呢,如果是能看懂我的意思修改xml也可以,只要满足需求,要解析成类对象,这能不能通过反序列化呢,还是手动解析的,怎么做,给出示例来谢谢,一定加分

------解决方案--------------------
为指定的XML文件生成类并反序列化
------解决方案--------------------
using System;
using System.Windows.Forms;
using System.Xml;

namespace xmlNode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("1.xml");
XmlNode xn = xmldoc.SelectSingleNode("items/rtnCode");
string s = xn.InnerText;
}
}
}


("items/rtnCode" 是节点位置

你还是直接解析吧
------解决方案--------------------
读取xml的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Xml;
namespace XmlAA
{
public class ReadXml
{
public static void Main()
{
XmlReaderSettings setting = new XmlReaderSettings();
//Insert Template.
setting.Schemas.Add(null,@"..\..\po.xsd");
//Validate.
setting.ValidationType = ValidationType.Schema;
//Does not handle errors, but displays an error message.
setting.ValidationEventHandler += new ValidationEventHandler(EvenHandler);

XmlReader reader = XmlReader.Create(@"..\..\Vol.xml",setting);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine(reader.Value);
}
}
Console.ReadLine();
}

public static void EvenHandler(object sender, ValidationEventArgs e)
{
//Error message
Console.WriteLine("The method or operation is not implemented.");
}
}
}

------解决方案--------------------
ok. 你是不是想把xml 解析成类?
那就要先将xml中的Step元素提取出来成字段封装成个对象
再做个类对象 只放List<Step> list=new List<Step>();

把现有的xml看作N个Step对象
接下来就是直接解析xml 装进对象
我个人觉得用循环嵌套循环更容易读取。我猜测你的配置文件是动态的 可增可删?
List<Step> list=new List<Step>();
XmlDataDocument xml=new XmlDataDocument();
xml.load("文件名");
XmlNode baseNode =xml.SelectSingleNode("/Config");
XmlNodeList listStep=baseNode.SelectNodes("Step");
for(int i=0;i<listStep.count;i++)
{
Step s=new Step();
s.StepName=listStep[i].Attributes[0].Value;
XmlNodeList list1=baseNode.SelectNodes("Step/"+s.StepName+"");
for(int j=0;j<list1.count;j++)
{
s.Name=list1[j].Attributes[0].Value;
s.Path=list1[j].Attributes[1].Value;
}
list.add(s);
}

我觉得这个应该可以 你试试 有问题再讨论

------解决方案--------------------
补充:List<Step> list=new List<Step>(); 这个作为个字段封装成 Config 类 的属性上面的就可以直接实例化