【问题标题】:Deserialize XML in a WP8 Application在 WP8 应用程序中反序列化 XML
【发布时间】:2013-03-18 15:31:31
【问题描述】:

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

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


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

这是我的 Quotes 课程:

[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;

        }  

这在每个具有这种结构的提要中都可以正常工作,其中包含一个 &lt;text&gt; 部分。但是我有很多&lt;text&gt;

如果我使用上面的 C# 代码,则只解析第一个 &lt;text&gt; 部分,其他部分将被忽略。我需要为单个 XML 提要中的每个 &lt;text&gt; 部分创建单独的 List 或 ObservableCollection。

【问题讨论】:

  • “这在每个具有这种结构的提要中都可以正常工作” - 你的意思是你有多个引号部分吗?
  • 显示Quote类代码。
  • 如果您创建一个具有您喜欢的结构的类并进行序列化会更容易。因此,该字符串将是您的 xml 所需要的。
  • Hassan 我有很多引号部分......只有当我有一个文本标签时,此代码才有效......谢谢
  • @user1931853 你只有一个属性(getter setter)可以带你文本标签。

标签: c# xml windows windows-phone-7 deserialization


【解决方案1】:

将您的 Quote 类更改为包含 List&lt;string&gt; text 而不是 string text

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

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

更新

由于您的应用程序中现有的功能和当前的Quote 类成员,我将离开序列化并使用 LINQ to XML 将数据从 XML 加载到 Quotes 类实例中:

XDocument document = XDocument.Parse(e.Result);
Quotes quotes = new Quotes() {
    Collection = document.Root
                         .Element("quotes")
                         .Elements("quote")
                         .Select(q => new {
                             xml = q,
                             Author = (string) q.Element("author")
                         })
                         .SelectMany(q => q.xml.Elements("text")
                                           .Select(t => new Quote() {
                                                author = q.Author,
                                                text = (string)t
                                            }))
                         .ToList()
};

我已经使用以下 QuotesQuote 类声明对其进行了测试:

public class Quotes
{
    public List<Quote> Collection { get; set; }
}

public class Quote
{
    public string author { get; set; }

    public string text { get; set; }
}

属性不再需要,因为这种方法不使用 XmlSerialization。

【讨论】:

  • 感谢您的回答。据说不能将'System.Collections.Generic.List'隐式转换为'string'所以我想我必须更改页面中的实现,我将在其中显示文本但我不知道如何你能帮忙吗?:引用报价;公共 QuotePage() { InitializeComponent(); var app = App.Current as App;报价= app.selectedQuote; // 在页面中显示报价详情 author.Text = quote.author;文本.文本 = 报价.文本; }
  • 您可能已经在代码中的某处设置了对该属性的引用。您能否更新您的问题并编写您希望从该 xml 实现的对象(以及它的内容)?它是一个Quote 类对象,其中包含作者和文本列表,还是List&lt;Quote&gt; 每个对象都有一个作者和一个来自xml 的文本?
  • 我在第一页有两页,我有所有作者,当我选择作者时,它会显示该作者的所有文本
  • 我照你说的做了,但还是不行……谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 2010-11-25
相关资源
最近更新 更多