0

我想在解析本地 XML 之前显示数据但不知道怎么做?我用这个试过了

 XDocument loadedData = XDocument.Load("Record.xml"); var data = from
 query in loadedData.Descendants("item")
        select new Person {
          Name = (string)query.Element("record"),
        }; listBox1.ItemsSource = data;

但它只显示一个元素而不显示标题名称。

XML 是:

 <root>
 <item name="Title 1">
   <record>record1</record>
   <record>record2</record>
</item>
   <item name="Title 2">
   <record>record1</record>
   <record>record2</record>
</item>
</root> 
4

1 回答 1

0

检查此代码:

            XDocument loadedData = XDocument.Load("Try.xml");
            var data = from query in loadedData.Descendants("item")
                       from a in query.Elements("record")
                       select new Person
            {
                Name = (string)a.Value

            };
            var array = data.ToArray();
于 2013-09-30T07:08:48.333 回答