0

我在将 xml 文件加载到 Observable Collection 时遇到问题。

我的 XML 看起来像

<?xml version="1.0" encoding="utf-8"?>
<Stock>
  <Assortment>
    <Item>Адельфан -  эзидрекс  таб  №10</Item>
    <Quantity>89</Quantity>
    <Price>3.0000</Price>
    <Summ>267</Summ>
    <Price1>3.0000</Price1>
    <Summ1>267</Summ1>
    <ValidDate>01.01.2031</ValidDate>
    <Manufacturer>КРКА</Manufacturer>
  </Assortment>
  <Assortment>
    <Item>Адельфан -  эзидрекс  таб  №10</Item>
    <Quantity>8</Quantity>
    <Price>3.0000</Price>
    <Summ>24</Summ>
    <Price1>3.0000</Price1>
    <Summ1>24</Summ1>
    <ValidDate>01.01.2019</ValidDate>
    <Manufacturer>КРКА</Manufacturer>
  </Assortment>
</Stock>

我试过的代码就是这个。

            XDocument xml = XDocument.Load(filename);
                foreach (XElement xe in xml.Elements("Assortment"))
                {
                    _dummyCollection1.Add(new DummyClass1()
                    {
                        Наименование = xe.Element("Item").Value,
                        Количество = xe.Element("Quantity").Value,
                        Цена = xe.Element("Price").Value,
                        Сумма = xe.Element("Summ").Value,
                        Цена1 = xe.Element("Price1").Value,
                        Сумма1 = xe.Element("Summ1").Value,
                        Срокгодности = xe.Element("ValidDate").Value,
                        Производитель = xe.Element("Manufacturer").Value
                    });
                }
                BuyDataGrid.ItemsSource = _dummyCollection1;

我得到空的DataGrid。如何将 xml 传递给 Observable Collection?

4

2 回答 2

2

我找到了如何做,以防万一有人需要;

                    XmlDocument doc = new XmlDocument();
                    doc.Load(filename);
                    XmlElement root = doc.DocumentElement;
                    XmlNodeList nodes = root.SelectNodes("Assortment"); 
                    foreach (XmlNode node in nodes)
                    {
                        _dummyCollection1.Add(new DummyClass1()
                        {
                            Наименование = node["Item"].InnerText,
                            Количество = node["Quantity"].InnerText,
                            Цена = node["Price"].InnerText,
                            Сумма = node["Summ"].InnerText,
                            Цена1 = node["Price1"].InnerText,
                            Сумма1 = node["Summ1"].InnerText,
                            Срокгодности = node["ValidDate"].InnerText,
                            Производитель = node["Manufacturer"].InnerText
                        });
                    }
                    BuyDataGrid.ItemsSource = _dummyCollection1;
于 2013-11-14T08:15:15.483 回答
0

您计划用作 ObservableCollection 集合对象的类必须实现INotifyPropertyChanged

这是我对如何为 dataGrid 填充模型的示例的回答

例子

于 2013-11-14T04:54:58.917 回答