从xml解析为对象此示例

时间:2016-03-30 09:09:14

标签: c# xml xml-serialization

我找到了很多例子,但不知道解析是如何停止的。

        <?xml version="1.0" encoding="UTF-8" ?>
        <park>
            <name>DEPO Name</name>
            <numberOfTrams>120</numberOfTrams>
            <minStopAtDepo>3:0</minStopAtDepo>
            <timeBetweenArrivals>0:5</timeBetweenArrivals>  
            <allowedSwithing>true</allowedSwithing>
            <maxDepartureDelay>0.3</maxDepartureDelay>
            <stops>
                <stop sname="A" minStopTime="0:30">
                    <connections>
                        <connection stopConnected="A*" time="3:0"></connection>             
                    </connections>
                </stop> 
                <stop sname="A*" minStopTime="0:30">
                    <connections>
                        <connection stopConnected="B*" time="3:0"></connection>             
                    </connections>
                </stop> 
                <stop sname="B" minStopTime="0:30">
                <connections>
                    <connection stopConnected="J" time="3:0"></connection>  
                    <connection stopConnected="GG*" time="2:0"></connection>
                </connections>
                </stop> 
<stop sname="T" minStopTime="0:30">
            <connections>
                <connection stopConnected="S" time="2:0"></connection>
                <connection stopConnected="T*" time="2:0"></connection>
                <connection stopConnected="U" time="2:0"></connection>
                <connection stopConnected="T" time="1:0"></connection>
            </connections>
        </stop>
        </stops>
  

结束停止,但将是不同文件上的停止点   然后开始路由器

            <routes>
    <route rname="A--B--C--D--E--G--H--I--J--K--L--M--N--O--P--Q--R--S--T" routeToDepo="A--B--C--D--E--G--H--I--J--K--L--M--N--O" routeFromDepo="O*--N*--M*--L*--K*--J*--I*--H*--G*--E*--F*--C*--B*--A*" backRoute="T*--S*--R*--Q*--P*--O*--N*--M*--L*--K*--J*--I*--H*--G*--E*--F*--C*--B*--A*">
        <numberOfTramsDuringTheDay>
            <zero>0</zero>  <!-- 0-0:59 -->
            <one>0</one>    <!-- 1-1:59 and so on -->
            <two>0</two>
            <three>0</three>
            <four>0</four>
            <five>0</five>
            <six>7</six>
            <seven>12</seven>
            <eight>15</eight>
            <nine>11</nine>
            <ten>11</ten>
            <eleven>10</eleven>
            <twelve>10</twelve>
            <thirteen>11</thirteen>
            <fourteen>11</fourteen>
            <fiveteen>10</fiveteen>
            <sixteen>10</sixteen>
            <seventeen>10</seventeen>
            <eighteen>10</eighteen>
            <nineteen>7</nineteen>
            <twenty>5</twenty>
            <twentyOne>5</twentyOne>
            <twentyTwo>4</twentyTwo>
            <twentyThree>3</twentyThree>                
        </numberOfTramsDuringTheDay>            
    </route>
ect.

我有depo,depo有停止(路由器) 我解析:             名称             numberOfTrams             minStopAtDepo             timeBetweenArrivals             allowedSwithing             maxDepartureDelay

我使用XMLReader

1 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                "<park>" +
                    "<name>DEPO Name</name>" +
                    "<numberOfTrams>120</numberOfTrams>" +
                    "<minStopAtDepo>3:0</minStopAtDepo>" +
                    "<timeBetweenArrivals>0:5</timeBetweenArrivals>" +
                    "<allowedSwithing>true</allowedSwithing>" +
                    "<maxDepartureDelay>0.3</maxDepartureDelay>" +
                    "<stops>" +
                        "<stop sname=\"A\" minStopTime=\"0:30\">" +
                            "<connections>" +
                                "<connection stopConnected=\"A*\" time=\"3:0\"></connection>" +
                            "</connections>" +
                        "</stop>" +
                        "<stop sname=\"A*\" minStopTime=\"0:30\">" +
                            "<connections>" +
                                "<connection stopConnected=\"B*\" time=\"3:0\"></connection>" +
                            "</connections>" +
                        "</stop>" +
                        "<stop sname=\"B\" minStopTime=\"0:30\">" +
                        "<connections>" +
                            "<connection stopConnected=\"J\" time=\"3:0\"></connection>" +
                            "<connection stopConnected=\"GG*\" time=\"2:0\"></connection>" +
                        "</connections>" +
                        "</stop>" +
                        "<stop sname=\"T\" minStopTime=\"0:30\">" +
                            "<connections>" +
                                "<connection stopConnected=\"S\" time=\"2:0\"></connection>" +
                                "<connection stopConnected=\"T*\" time=\"2:0\"></connection>" +
                                "<connection stopConnected=\"U\" time=\"2:0\"></connection>" +
                                "<connection stopConnected=\"T\" time=\"1:0\"></connection>" +
                            "</connections>" +
                        "</stop>" +
                    "</stops>" +
                "</park>";

            XDocument doc = XDocument.Parse(xml);

            var results = doc.Descendants("stops").FirstOrDefault().Elements("stop").Select(x => new
            {
                sname = x.Attribute("sname").Value,
                minStopTime = x.Attribute("minStopTime").Value,
                connections = x.Descendants("connection").Select(y => new {
                    stopConnected = y.Attribute("stopConnected").Value,
                    time = y.Attribute("time").Value
                }).ToList()
            }).ToList();
        }
    }
}