XML解析 - 读一个简单的XML文件和检索值简单、文件、XML

2023-09-02 01:39:26 作者:萌哒哒的小二货°

我写了一个任务调度程序,学习的目的。目前,我节省了预定的任务只是为纯文本,然后分析它使用正则表达式。这看起来凌乱(code明智的),是不是很连贯。

我想加载从一个XML文件,而不是预定的任务,我已经搜索相当多寻找一些解决方案,但我不能让它的工作,我怎么想。

我写的结构是这样来存储我的数据的XML文件:

 <任务>
    <任务>
        <名称>停机和LT; /名称>
        &LT;地点&gt; C:/WINDOWS/system32/shutdown.exe< /地点&gt;
        &LT;参数&GT; -s -f -t 30℃/参数&GT;
        &LT; RunWhen&GT;
            &其中;时间&gt; 8:00:00上午10:00&所述; /时间&gt;
            &LT;日期&GT; 18/03/2011&LT; /日期&GT;
            &LT;天&GT;
                &LT;周一&GT;假&LT; /星期一&GT;
                &LT;周二&GT;假&LT; /周二&GT;
                &LT;周三,GT;假&LT; /周三,GT;
                &LT;周四&GT;假&LT; /周四&GT;
                &LT;周五&GT;假&LT; /周五&GT;
                &LT;周六和GT;假&LT; /周六和GT;
                &LT;星期日及GT;假&LT; /周日&GT;
                &LT;每天&GT;真&LT; /每天&GT;
                &LT;的RunOnce&GT;假&LT; /的RunOnce&GT;
            &LT; /天&GT;
        &LT; / RunWhen&GT;
        &LT;启用&GT;真&LT; /启用&GT;
    &LT; /任务&GT;
&LT; /任务&GT;
 

我想分析数据的方式是像这样:

     开启Tasks.xml   在加载的第一个任务标记。   在任务检索名称,位置和参数变量的值。   然后打开RunWhen标签和检索时间和日期标记的值。   在打开的日子标签和检索中的各个变量的值。   检索启用。值   在加载下一个任务,并重复步骤3 - > 7,直到任务所有的任务标签被解析   

我很相信你能做到这样,我只是不能工作了,因为有这么多不同的方式来做事的XML我有点不知所措。但是我走这么远是我最有可能被使用的XPathDocument和声明XPathNodeIterator吧?

如果有人能告诉我一个例子,或向我解释如何做到这一点做,我会很高兴。

解决方案

简单的方法来解析XML是使用的 LINQ到XML

63 简XML文件详解以及解析

例如你有下面的XML文件

 &LT;库&GT;
 &所述轨迹ID =1流派=斥责时间=3点24&GT;
&LT;名称&gt;关于我们要RMX(2Pac的壮举。)&LT; /名称&gt;
&LT;艺术家&GT; DMX&LT; /艺术家&GT;
&LT;画册&gt;将DOGZ Mixtape作者:?!谁是下一个&LT; /专辑&GT;
&LT; /曲目&GT;
 &所述轨迹ID =2流派=斥责时间=5时06&GT;
 &LT;名称&gt;天使(英尺里贾纳钟。)LT; /名称&gt;
 &LT;艺术家&GT; DMX&LT; /艺术家&GT;
 &LT;画册&GT; ...然后有X  -  LT; /专辑&GT;
 &LT; /曲目&GT;
 &LT;轨道ID =3流派=破节拍时间为6:16&GT;
 &LT;名称&gt;梦你的梦想和LT; /名称&gt;
  &LT;艺术家&GT;混合&LT; /艺术家&GT;
 &LT;画册&GT;广角&LT; /专辑&GT;
&LT; /曲目&GT;
&LT;轨道ID =4流派=破节拍时间为9:38&GT;
&LT;名称&gt;成品交响乐团&LT; /名称&gt;
&LT;艺术家&GT;混合&LT; /艺术家&GT;
&LT;画册&GT;广角&LT; /专辑&GT;
&LT; /曲目&GT;
&LT; /库&GT;
 

有关读取这个文件,你可以用下面的code:

 公共无效读(字符串文件名)
{
    的XDocument DOC = XDocument.Load(文件名);

    的foreach(的XElement EL在doc.Root.Elements())
    {
        Console.WriteLine({0} {1},el.Name,el.Attribute(ID)值。);
        Console.WriteLine(属性:);
        的foreach(XAttribute ATTR在el.Attributes())
            Console.WriteLine({0},attr)使用;
        Console.WriteLine(元素:);

        的foreach(在el.Elements的XElement元件())
            Console.WriteLine({0}:{1},element.Name,element.Value);
    }
}
 

I've written a Task Scheduling program for learning purposes. Currently I'm saving the scheduled tasks just as plain text and then parsing it using Regex. This looks messy (code wise) and is not very coherent.

I would like to load the scheduled tasks from an XML file instead, I've searched quite a bit to find some solutions but I couldn't get it to work how I wanted.

I wrote an XML file structured like this to store my data in:

<Tasks>
    <Task>
        <Name>Shutdown</Name>
        <Location>C:/WINDOWS/system32/shutdown.exe</Location>
        <Arguments>-s -f -t 30</Arguments>
        <RunWhen>
            <Time>8:00:00 a.m.</Time>
            <Date>18/03/2011</Date>
            <Days>
                <Monday>false</Monday>
                <Tuesday>false</Tuesday>
                <Wednesday>false</Wednesday>
                <Thursday>false</Thursday>
                <Friday>false</Friday>
                <Saturday>false</Saturday>
                <Sunday>false</Sunday>
                <Everyday>true</Everyday>
                <RunOnce>false</RunOnce>
            </Days>
        </RunWhen>
        <Enabled>true</Enabled>
    </Task>
</Tasks>

The way I'd like to parse the data is like so:

Open Tasks.xml Load the first Task tag. In that task retrieve the values of the Name, Location and Arguments tags. Then open the RunWhen tag and retrieve the values of the Time and Date tags. After that open the Days tag and retrieve the value of each individual tag within. Retrieve the value of Enabled. Load the next task and repeat steps 3 -> 7 until all the Task tags in Tasks have been parsed.

I'm very sure you can do it this way I just can't work it out as there are so many different ways to do things in XML I got a bit overwhelmed. But what I've go so far is that I would most likely be using XPathDocument and XPathNodeIterator right?

If someone can show me an example or explain to me how this would be done I would be very happy.

解决方案

Easy way to parse the xml is to use the LINQ to XML

for example you have the following xml file

<library>
 <track id="1" genre="Rap" time="3:24">
<name>Who We Be RMX (feat. 2Pac)</name>
<artist>DMX</artist>
<album>The Dogz Mixtape: Who's Next?!</album>
</track>
 <track id="2" genre="Rap" time="5:06">
 <name>Angel (ft. Regina Bell)</name>
 <artist>DMX</artist>
 <album>...And Then There Was X</album>
 </track>
 <track id="3" genre="Break Beat" time="6:16">
 <name>Dreaming Your Dreams</name>
  <artist>Hybrid</artist>
 <album>Wide Angle</album>
</track>
<track id="4" genre="Break Beat" time="9:38">
<name>Finished Symphony</name>
<artist>Hybrid</artist>
<album>Wide Angle</album>
</track>
</library>

For reading this file, you can use the following code:

public void Read(string  fileName)
{
    XDocument doc = XDocument.Load(fileName);

    foreach (XElement el in doc.Root.Elements())
    {
        Console.WriteLine("{0} {1}", el.Name, el.Attribute("id").Value);
        Console.WriteLine("  Attributes:");
        foreach (XAttribute attr in el.Attributes())
            Console.WriteLine("    {0}", attr);
        Console.WriteLine("  Elements:");

        foreach (XElement element in el.Elements())
            Console.WriteLine("    {0}: {1}", element.Name, element.Value);
    }
}