在 WP8 应用程序中反序列化 XML

2023-12-29

我正在尝试开发一个 Windows Phone 8 应用程序(我是 wp8 开发人员的新手)。

我有一个如下所示的 XML 文件:


<?xml version="1.0" ?> 
<root>
   <quotes>
      <quote>
         <author></author>
         <text></text>
         <text></text>
         <text></text>
      </quote>
   </quotes>
</root>

这是我的行情课:

[XmlRoot("root")]
public class Quotes
{
   [XmlArray("quotes")]
   [XmlArrayItem("quote")]
   public ObservableCollection<Quote> Collection { get; set; }
}

这是引用类:

public class Quote
{
   [XmlElement("author")]
   public string author { get; set; }

   [XmlElement("text")]
   public string text { get; set; }
}

然后我使用这段代码来反序列化它:

XmlSerializer serializer = new XmlSerializer(typeof(Quotes));
XDocument document = XDocument.Parse(e.Result);
Quotes quotes = (Quotes) serializer.Deserialize(document.CreateReader());
quotesList.ItemsSource = quotes.Collection;

// selected Quote
        Quote quote;

        public QuotePage()
        {
            InitializeComponent();

            // get selected quote from App Class
            var app = App.Current as App;
            quote = app.selectedQuote;

            // show quote details in page
            author.Text = quote.author;
            text.Text = quote.text;

        }  

这在具有这种结构的每种饲料中都可以很好地工作<text>部分。但我有很多饲料<text>

如果我使用上面的 C# 代码,则只需首先<text>部分被解析,其他部分被忽略。我需要为每个创建单独的 List 或 ObservableCollection<text>单个 XML 提要中的部分。


改变你的Quote包含的类List<string> text代替string text:

public class Quote
{
    [XmlElement("author")]
    public string author { get; set; }

    [XmlElement("text")]
    public List<string> text { get; set; }
}

Update

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WP8 应用程序中反序列化 XML 的相关文章

随机推荐