使用XmlReader反序列化自定义对象

时间:2013-11-27 15:49:01

标签: c# .net

我有一个像这样的巨大XML文件:

<Tests>
    <Test>
        <Code>a</Code>
        <Destination>test a</Destination>
        <Coordinate>
            <Latitude>0.0</Latitude>
            <Longitude>0.0</Longitude>
        </Coordinate>
        <Images>
            <ImageURL>1. url 1</ImageURL>
            <ImageURL>1. url 2</ImageURL>
            <ImageURL>1. url 3</ImageURL>   
        </Images>   
    </Test>
    <Test>
        <Code>b</Code>
        <Destination>test b</Destination>
        <Coordinate>
            <Latitude>0.0</Latitude>
            <Longitude>0.0</Longitude>
        </Coordinate>
        <Images>
            <ImageURL>2. url 1</ImageURL>
            <ImageURL>2. url 2</ImageURL>
            <ImageURL>2. url 3</ImageURL>
            <ImageURL>2. url 4</ImageURL>
            <ImageURL>2. url 5</ImageURL>           
        </Images>   
    </Test>

    ...

</Tests>

我试试这个

reader = XmlReader.Create("file");
while (reader.Read())
    if (reader.NodeType == XmlNodeType.Element)
    {
        switch (reader.Name)
        {
            case "Test":
            {
                .....
                Test elem = (Test)DeSerializerDestination.Deserialize(reader);
                .....
            } break;
            default: reader.Skip(); break;
        }
    }


private static readonly XmlSerializer DeSerializerTest = new XmlSerializer(typeof(Test));

public class Test
{
    private string _Code = string.Empty;
    public string Code
    {
        get { return _Code; }
        set { _Code = value; }
    }           
    private string _Destination = string.Empty;
    public string Destination
    {
        get { return _Destination; }
        set { _Destination = value; }
    }           
    private Coordinate _Coordinates = new Coordinate();
    public Coordinate Coordinates
    {
        get { return _Coordinates; }
        set { _Coordinates = value; }
    }           
    private ImageUrl[] _ImageUrl;
    public ImageUrl[] Images
    {
        get { return _ImageUrl; }
        set { _ImageUrl = value; }
    }       

}   

public class Coordinate
{
    private string _Latitude = string.Empty;
    public string Latitude
    {
        get { return _Latitude; }
        set { _Latitude = value; }
    }
    private string _Longitude = string.Empty;
    public string Longitude
    {
        get { return _Longitude; }
        set { _Longitude = value; }
    }
}

public class ImageUrl
{
    private string _ImageURL = string.Empty;
    public string ImageURL
    {
        get { return _ImageURL; }
        set { _ImageURL = value; }
    }
}

对象elem包含但不是全部的信息;财产&#34;图像&#34;总是空的。 我认为这是因为我没有初始化这个private ImageUrl[] _ImageUrl;,但是我会初始化它,考虑到我不知道图像的数量?我需要提一下,我必须使用.net 2.0。

更新:如果我在&#34;图像&#34;我总是得到第一个图片网址。

public class ImageUrl
....
System.Xml.Serialization.XmlElementAttribute("ImageURL")]
public string ImageURL
...

public class Test
...
System.Xml.Serialization.XmlElementAttribute("Images")]
public ImageUrl[] Images
...

更新:我用这个

 [System.Xml.Serialization.XmlArray("Images")]
 [System.Xml.Serialization.XmlArrayItem("ImageURL")]
 public List<ImageUrl> Images
 {
    get { return _ImageUrl; }
    set { _ImageUrl = value; }
 }

现在我得到了列表但是#34; ImageUrl&#34;是空的,不包含&#34; 1。网址1&#34; ....

这是决议

 [System.Xml.Serialization.XmlIgnore]
 private List<string> images = new List<string>();
 [System.Xml.Serialization.XmlArray("Images")]
 [System.Xml.Serialization.XmlArrayItem("ImageURL")]
 public List<string> Images
  {
    get { return images; }
    set { images = value; }
  }

1 个答案:

答案 0 :(得分:2)

尝试以下操作,将Xml序列化到您的对象中,为您提供与Xml结构匹配的对象。

YourObject oObject = new YourObject ();

try
{
   XmlSerializer oSerializer = new XmlSerializer(typeof(YourObject));

    using (StringReader oReader = new StringReader(XmlString)) 
    {
       oObject = (YourObject)oSerializer.Deserialize(oReader);
    }
}
catch
{
  ...
}