用的是linq to xml
结构:
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<grRoot Text=" xx">
<gr Text="xxx">
<Row isdisplay="1" title="01收据_gr" src="C:\DOC\FSD\GR\Rar\gr\01收据.doc" emptysrc="" ></Row>
</gr>
<grHighest Text="个人20万以上">
<Row isdisplay="1" title="01收据_grHighest" src="C:\DOC\FSD\GR\Rar\grHighest\01收据.doc" emptysrc=""></Row>
</grHighest>
<!-- 公共的配置-->
<Row isdisplay="1" title="01_grRoot" src="C:\DOC\FSD\GR\Rar\01.doc" emptysrc=""></Row>
</grRoot>
</Data>
希望读取:grRoot 下面的gr 或者grHighest 这2个是参数。加上最后一条 这个是公共的。
取他的title 和src
code:
var xDoc = XDocument.Load("E:\\doc\\QExport.xml"); var query = from item in xDoc.Element("Data").Element("grRoot").Elements("gr") select new { Name = item.Name, Value = item.Value, Src = item.Attribute("Src").Value }; //如何加上 公共的配置呢? foreach (var item in query) { Console.WriteLine("{0} -------- {1}--------------------- {2}", item.Name, item.Value , item.Src); }
static void Main(string[] args) { Loadxml(); Console.ReadKey(); } private static void Loadxml() { string title = ""; string src = ""; XDocument xdoc = XDocument.Load(@"F:\C\增删改查功能练习\studyXml\studyXml\bin\Debug\sb.xml"); XElement e = xdoc.Element("Data"); XElement el = e.Element("grRoot"); foreach (XElement item in el.Elements()) { if (item.Name.LocalName == "gr") { title = item.Element("Row").Attribute("title").Value; src = item.Element("Row").Attribute("src").Value; Console.WriteLine(string.Format("gr : title = {0} ------- src = {1}",title,src)); } if (item.Name.LocalName == "grHighest") { title = item.Element("Row").Attribute("title").Value; src = item.Element("Row").Attribute("src").Value; Console.WriteLine(string.Format("gr : title = {0} ------- src = {1}", title, src)); } if (item.Name.LocalName == "Row") { title = item.Attribute("title").Value; src = item.Attribute("src").Value; Console.WriteLine(string.Format("gr : title = {0} ------- src = {1}", title, src)); } } }
这个问题很简单的,怎么没人回答?是因为太简单大家不想回答么?
(我的代码可能比较累赘哈~)