XML文件转换为C#对象

时间:2018-07-10 08:20:25

标签: c# .net xml

我必须在C#对象中读取此XML文件。我在一个对象中有这些信息。
并非所有标签都很重要!  

这些将是我在对象中需要的重要信息:
 -与staStart属性对齐
 -在这些主题中具有线螺旋和曲线的CoordGoem只是重要的属性

XML文件

<Alignments name="">
    <Alignment name="AL-voor omvorming landxml" length="6550.000000000015" staStart="30000." desc="">
        <CoordGeom>
            <Line dir="0." length="100.">
                <Start>175.282796686952 -1708.474133524573</Start>
                <End>175.282796686952 -1608.474133524573</End>
            </Line>
            <Spiral length="100." radiusEnd="500." radiusStart="INF" rot="cw" spiType="clothoid" theta="5.729577951308" totalY="3.330953138396" totalX="99.900046285614" tanLong="66.701620764677" tanShort="33.365112106501">
                <Start>175.282796686952 -1608.474133524573</Start>
                <PI>175.282796686952 -1541.772512759896</PI>
                <End>171.951843548555 -1508.574087238959</End>
            </Spiral>
            <Curve rot="cw" chord="99.833416646828" crvType="arc" delta="11.459155902616" dirEnd="342.811266146075" dirStart="354.270422048692" external="2.510459200228" length="100." midOrd="2.497917360987" radius="500." tangent="50.167336042725">
                <Start>171.951843548555 -1508.574087238959</Start>
                <Center>-325.550239090457 -1558.490795562373</Center>
                <End>152.118005472345 -1410.730692231703</End>
                <PI>166.94346698734 -1458.657378915545</PI>
            </Curve>
            <Spiral length="100." radiusEnd="INF" radiusStart="500." rot="cw" spiType="clothoid" theta="5.729577951308" totalY="3.330953138396" totalX="99.900046285614" tanLong="66.701620764677" tanShort="33.365112106501">
                <Start>152.118005472345 -1410.730692231703</Start>
                <PI>142.257940647353 -1378.855783172596</PI>
                <End>116.283106059873 -1317.419522049479</End>
            </Spiral>

对象

public class LandXMLAlignments
{
    public long staStart { get; set; }
    [XmlArray("CoordGeom")]
    public List<LandXMLAlignmentsCoordGeom> CoordGeoms { get; set; }
}

public class LandXMLAlignmentsCoordGeom
{
    public List<LandXMLAlignmentsCoordGeomLine> CoordGeomLines { get; set; }
    public List<LandXMLAlignmentsCoordGeomSpiral> CoordGeomSpiral { get; set; }
    public List<LandXMLAlignmentsCoordGeomLine> CoordGeomCurve { get; set; }
}
[System.Xml.Serialization.XmlType("Line", IncludeInSchema = true)]
public class LandXMLAlignmentsCoordGeomLine
{
    public int length { get; set; }
    public int position { get; set; }
}

[System.Xml.Serialization.XmlType("Spiral", IncludeInSchema = true)]
public class LandXMLAlignmentsCoordGeomSpiral
{
    public int position { get; set; }
    public int length { get; set; }
    public string radiusStart { get; set; }
    public string radiusEnd { get; set; }
    public string rot { get; set; }
    public string spiType { get; set; }
}
[System.Xml.Serialization.XmlType("Curve", IncludeInSchema = true)]
public class LandXMLAlignmentsCoordGeomCurve
{
    public int position { get; set; }
    public int length { get; set; }
    public int radiusStart { get; set; }
    public string rot { get; set; }
}

我会从中提取该对象,但是现在我不知道如何实际读取该对象中的xml文件。

3 个答案:

答案 0 :(得分:2)

假设您的类还可以,您可以创建一个助手类来执行从XML到POCO对象的转换,如下所示:

public static class XmlHelper
{
    public static T ParseXmlFile<T>(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        using (TextReader reader = new StreamReader(filePath))
        {
            T configuration = (T)serializer.Deserialize(reader);
            return configuration;
        }
    }
}

并像这样使用它:

var  alignments = XmlHelper.ParseXmlFile<LandXMLAlignments>(YourXMLPath);

答案 1 :(得分:1)

尝试以下操作:

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


namespace ConsoleApplication51
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Alignments));
            Alignments alignments = (Alignments)serializer.Deserialize(reader);

        }
    }
    public class Alignments
    {
        [XmlElement("Alignment")]
        public Alignment alignment { get; set; }
    }
    public class Alignment
    {
        public long staStart { get; set; }
        [XmlElement("CoordGeom")]
        public List<LandXMLAlignmentsCoordGeom> CoordGeoms { get; set; }
    }

    public class LandXMLAlignmentsCoordGeom
    {
        [XmlElement("Line")]
        public List<LandXMLAlignmentsCoordGeomLine> CoordGeomLines { get; set; }
        [XmlElement("Spiral")]
        public List<LandXMLAlignmentsCoordGeomSpiral> CoordGeomSpiral { get; set; }
        [XmlElement("Curve")]
        public List<LandXMLAlignmentsCoordGeomLine> CoordGeomCurve { get; set; }
    }
    [System.Xml.Serialization.XmlType("Line", IncludeInSchema = true)]
    public class LandXMLAlignmentsCoordGeomLine
    {
        public int length { get; set; }
        public int position { get; set; }
    }

    [System.Xml.Serialization.XmlType("Spiral", IncludeInSchema = true)]
    public class LandXMLAlignmentsCoordGeomSpiral
    {
        public int position { get; set; }
        public int length { get; set; }
        public string radiusStart { get; set; }
        public string radiusEnd { get; set; }
        public string rot { get; set; }
        public string spiType { get; set; }
    }
    [System.Xml.Serialization.XmlType("Curve", IncludeInSchema = true)]
    public class LandXMLAlignmentsCoordGeomCurve
    {
        public int position { get; set; }
        public int length { get; set; }
        public int radiusStart { get; set; }
        public string rot { get; set; }
    }

}

答案 2 :(得分:0)

如果您使用的是Visual Studio,请执行以下操作:

  1. 复制XML
  2. 选择“编辑菜单”,
  3. 特殊粘贴,将XML粘贴为类。

Reference