【问题标题】:XML Elements to LinqXML 元素到 Linq
【发布时间】:2018-03-16 20:19:37
【问题描述】:

我正在尝试将我需要的所有信息从 XML 显示到 datagridview1。 XML 有一个<Product> 标记,其中包含它自己的属性和几个具有它自己的属性的元素。

看起来像这样:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Import>
    <Products>
        <Product Id="1" Name="prodcut1">
            <Prices price=10,10/>
            <Barcodes barcode="123123123123"/>
        </Product>

        <Product Id="2" Name="Product2">
            <Prices price=9,9/>
            <Barcodes Barcode="123123123123"/>
        </Product>


    </Products>

</Import>

据我所知,由于 datagridview 不能显示多个表(即dataGrid1.DataSource = dataSet.Tables[1];),因此我尝试使用 Linq 和匿名类型来选择我想要的任何内容,并在每个 &lt;Product&gt; 标签的一行中显示. 在数据网格上,它应该看起来像这样:

id | Name         | Barcode | Price
1   product1        123...      10,10
2   product2        123....     9,9

到目前为止,我写这篇文章是为了展示 &lt;Product&gt; 及其属性,“id”和“name”:

XElement ProdXML = XElement.Load(@"C:\Export\ppp.xml");
var query = from st in ProdXML.Descendants("Product")
            select new
            {
                Id = st.Attribute("Id").Value,
                name = st.Attribute("Name").Value,

             };
dataGridView1.DataSource = query.ToList();

有没有办法在Select 中检索Prices =&gt; Price 和Barcodes =&gt; barcode?

【问题讨论】:

  • 嗯,是的,使用st.Element("Prices") 来获取Prices 元素等。请注意,您的XML 当前无效,我真的希望 它会使用@ 987654333@ 作为小数点分隔符,而不是 ,。

标签: c# xml linq


【解决方案1】:

是的,你只需要访问元素:

XElement ProdXML = XElement.Load(@"C:\Export\ppp.xml");
            var query = from st in ProdXML.Descendants("Product")

select new
{
    Id = st.Attribute("Id").Value,
    name = st.Attribute("Name").Value,
    price = st.Element("Prices").Attribute("Price").Value,
    barcode = st.Element("Barcodes").Attribute("Barcode").Value
};
dataGridView1.DataSource = query.ToList();

【讨论】:

    【解决方案2】:

    尝试使用xml反序列化器,它会更容易使用。然后您可以转换为列表对象并使用它。

    类似:

    string xml = "";//Get xml here            
    var serializer = new XmlSerializer(typeof(Import));
    var import = (Import)serializer.Deserialize(xml);
    

    使用System.Xml.Serialization命名空间

    你可以使用的类可以是:

    [XmlRoot(ElementName = "Prices")]
    public class Prices
    {
        [XmlAttribute(AttributeName = "price")]
        public string Price { get; set; }
    }
    
    [XmlRoot(ElementName = "Barcodes")]
    public class Barcodes
    {
        [XmlElement(ElementName = "barcode")]
        public List<string> Barcode { get; set; }
    }
    
    [XmlRoot(ElementName = "Product")]
    public class Product
    {
        [XmlElement(ElementName = "Prices")]
        public Prices Prices { get; set; }
        [XmlElement(ElementName = "Barcodes")]
        public Barcodes Barcodes { get; set; }
        [XmlAttribute(AttributeName = "Id")]
        public string Id { get; set; }
        [XmlAttribute(AttributeName = "Name")]
        public string Name { get; set; }
    }
    
    [XmlRoot(ElementName = "Products")]
    public class Products
    {
        [XmlElement(ElementName = "Product")]
        public List<Product> Product { get; set; }
    }
    
    [XmlRoot(ElementName = "Import")]
    public class Import
    {
        [XmlElement(ElementName = "Products")]
        public Products Products { get; set; }
    }
    

    【讨论】:

    • 如果我需要和他们一起玩,我会记住这一点,现在我只需要显示这些值,所以 Sebastian 解决方案到目前为止工作正常。非常感谢。
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多