【问题标题】:How to extract attributes from array using XmlSerializer?如何使用 XmlSerializer 从数组中提取属性?
【发布时间】:2012-07-27 20:00:33
【问题描述】:

我想使用 XmlSerializer 提取图像属性“href”。

如果我的设置如下所示,它将起作用:

<images>
       <image id="285">  
          http://images1.com/test.jpg" 
       </image>

       <image id="286">
          http://images1.com/test.jpg"
       </image>       
</images>

但如果它看起来像这样则不是:

<images>
    <image href=http://images1.com/test.jpg" id="285" />
    <image href=http://images1.com/test.jpg" id="286" />        
</images>

这是我的对象

   private string[] imageList;
   [XmlArrayItem("image", typeof(object))]
   [XmlArray("images")]

    public string[] imageLink
    {
        get
        {
            return imageList;
        }
        set
        {
            imageList = value;
        }

    }

【问题讨论】:

  • 您是否考虑过为此使用 Linq to Xml 而不是序列化程序?
  • @JamieSee 是的,我有,我正在使用 XBOX XDK,我不相信 XBOX 上支持 Linq。
  • @JamieSee ,发现这也可以用 XDocument 来完成。
  • 是的,它可以,而且使用 XDcoument 实际上更容易。我不建议这样做,因为它是 System.Xml.Linq 的一部分。

标签: c# xml-serialization deserialization xmlserializer xml-deserialization


【解决方案1】:

我尝试了多种方法来尝试让序列化程序符合此 XML,但运气不佳。

你可能只想做这样的事情:

    string xml = @"<images>
    <image href=""http://images1.com/test.jpg"" id=""285"" />
    <image href=""http://images1.com/test2.jpg"" id=""286"" />        
</images>";

    List<string> images = new List<string>();
    using (StringReader sr = new StringReader(xml))
    using (XmlTextReader xr = new XmlTextReader(sr))
    {
        while (!xr.EOF)
        {
            xr.MoveToContent();
            xr.ReadToDescendant("image");
            xr.MoveToAttribute("href");
            xr.ReadAttributeValue();            
            images.Add(xr.Value);
            xr.MoveToElement();
            if (xr.Name != "images")
            {
                xr.ReadElementString();
            }
            else
            {
                xr.ReadEndElement();
            }
        }
    }

我做了更多的探索,想出了一种使用序列化并获得所需 XML 的方法:

[XmlRoot("images")]
public class ImageListWrapper
{
    public ImageListWrapper()
    {
        Images = new List<Image>(); 
    }

    [XmlElement("image")]
    public List<Image> Images
    {
        get; set;
    }

    public List<string> GetImageLocations()
    {
        List<string> imageLocations = new List<string>();

        foreach (Image image in Images)
        {
            imageLocations.Add(image.Href);
        }

        return imageLocations;
    }
}

[XmlRoot("image")]
public class Image
{
    [XmlAttribute("href")]
    public string Href { get; set; }
}

【讨论】:

  • 是的,看来我要么必须重构我的 XML,要么使用您提供的这段漂亮的代码。非常感谢,同时我将看看是否有人可以提供 XML 序列化程序解决方案。
  • 今天早上我用它做了更多的工作,并找到了一种可以使用您的确切 XML 进行序列化的方法。
  • 我想出了同样的故障,但错过了几个步骤。谢谢你,你是一个救生员。
  • 当我反序列化时,我是否反序列化输入:ImageListWrapper?
  • 是的,没错。 ImageListWrapper.Images 包含图像列表。您可以从那里获取 href 内容,即imageListWrapper.Images[0].Href。如果您愿意,将public List&lt;string&gt; GetImageLocations() 添加到 ImageWrapper 类应该相当简单。
猜你喜欢
  • 2021-07-17
  • 2022-10-08
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
相关资源
最近更新 更多