XML 解析 - 读取简单的 XML 文件并检索值 [英] XML Parsing - Read a Simple XML File and Retrieve Values

查看:31
本文介绍了XML 解析 - 读取简单的 XML 文件并检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个用于学习目的的任务调度程序.目前,我将计划任务保存为纯文本,然后使用 Regex 对其进行解析.这看起来很乱(代码明智)并且不是很连贯.

我想从 XML 文件加载计划任务,我已经搜索了很多以找到一些解决方案,但我无法让它按照我想要的方式工作.

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

<任务><任务><名称>关机</名称><Location>C:/WINDOWS/system32/shutdown.exe</Location><参数>-s -f -t 30</参数><运行时间><时间>上午 8:00:00</时间><日期>18/03/2011</日期><天数><星期一>假</星期一><星期二>假</星期二><星期三>假</星期三><星期四>假</星期四><星期五>假</星期五><星期六>假</星期六><周日>假</周日><每天>真</每天><RunOnce>false</RunOnce></天></RunWhen><启用>真</启用></任务></任务>

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

<块引用>

  1. 打开 Tasks.xml
  2. 加载第一个 Task 标签.
  3. 在该任务中检索 Name、Location 和 Arguments 标签的值.
  4. 然后打开 RunWhen 标记并检索时间和日期标记的值.
  5. 然后打开 Days 标签并检索其中每个单独标签的值.
  6. 检索 Enabled 的值.
  7. 加载下一个任务并重复步骤 3 -> 7,直到解析完 Tasks 中的所有 Task 标签.

我非常确定您可以通过这种方式做到这一点,但我无法解决,因为在 XML 中有很多不同的方法可以做事,我有点不知所措.但到目前为止,我很可能会使用 XPathDocument 和 XPathNodeIterator 对吗?

如果有人可以向我展示示例或向我解释如何做到这一点,我会非常高兴.

解决方案

解析 xml 的简单方法是使用 LINQ to XML

例如你有以下xml文件

<track id="1"genre="Rap" time="3:24"><name>我们是谁 RMX (feat. 2Pac)</name><艺术家>DMX</艺术家><专辑>The Dogz Mixtape:下一个是谁?!</album></track><track id="2"genre="Rap" time="5:06"><name>Angel (ft. Regina Bell)</name><艺术家>DMX</艺术家><专辑>...然后有X</专辑></track><track id="3"genre="Break Beat" time="6:16"><name>做梦</name><艺术家>混合</艺术家><专辑>广角</专辑></track><track id="4"genre="Break Beat" time="9:38"><name>完成的交响乐</name><艺术家>混合</艺术家><专辑>广角</专辑></track><图书馆>

为了读取这个文件,你可以使用以下代码:

public void Read(string fileName){XDocument doc = XDocument.Load(fileName);foreach(doc.Root.Elements() 中的 XElement el){Console.WriteLine("{0} {1}", el.Name, el.Attribute("id").Value);Console.WriteLine(" 属性:");foreach(el.Attributes() 中的 XAttribute attr)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:

  1. Open Tasks.xml
  2. Load the first Task tag.
  3. In that task retrieve the values of the Name, Location and Arguments tags.
  4. Then open the RunWhen tag and retrieve the values of the Time and Date tags.
  5. After that open the Days tag and retrieve the value of each individual tag within.
  6. Retrieve the value of Enabled.
  7. 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);
    }
}

这篇关于XML 解析 - 读取简单的 XML 文件并检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆