XElement - 无法捕获下一个节点

XElement - not able to capture the next node

我有以下 xml 个文件:

        <?xml version='1.0'?>
                <Products>
                <Product>
                  <Product_id>1100</Product_id>
                  <Product_name>xyz</Product_name>                       
               </Product>
               <Product>
                  <Product_id>1101</Product_id>
                  <Product_name>abc</Product_name>                       
               </Product><Product>
                  <Product_id>1102</Product_id>
                  <Product_name>def</Product_name>                       
               </Product>
               </Products>

我正在尝试获取每个属性的值,但我只获取了值,而不是所有 them.Any 关于如何更正此问题的 3 个指针?

foreach (XElement xe in xdoc.Descendants().Elements("Product"))
            {

                obj.status = xe.Element("Product_id"). Value;
                obj.file_id = xe.Element("Product_name").Value;
                productlist.Add(obj);

            }

除了第一个产品 "xyz" 之外,上面的循环没有遍历所有属性。

您需要为每个循环创建一个新对象。现在你只保存最后一项

    class Program
    {
        static void Main(string[] args)
        {
            List<Object> productlist = new List<Object>();

            foreach (XElement xe in xdoc.Descendants().Elements("Product"))
            {
                Object obj = new Object();
                obj.status = xe.Element("Product_id").Value;
                obj.file_id = xe.Element("Product_name").Value;
                productlist.Add(obj);

            }
        }
    }
    public class Object
    {
        public string status { get; set; }
        public string file_id { get; set; }
    }