添加到嵌套的xml元素

时间:2017-04-02 12:14:09

标签: c# xml

我有以下代码,想知道如何获得“sold-product”元素并添加产品。

XDocument xmlDoc = new XDocument();
xmlDoc.Add(new XElement("users"));
var xml = xmlDoc.Root;

foreach (var user in users)
{
    xml.Add(new XElement("user", new XAttribute("first-name", user.FirstName?? ""), new XAttribute("last-name", user.LastName?? ""),
    new XElement("sold-products")));

    foreach (var product in user.Products)
    {
        .Add(new XElement("Product",
            new XElement("name", product.Name),
            new XElement("price", product.Price)));
    }
}

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            XDocument xmlDoc = new XDocument();
            XElement users = new XElement("users");
            xmlDoc.Add(users);

            foreach (var user in cUser.users)
            {
               XElement newUser =  new XElement("user", 
                   new XAttribute("first-name", user.FirstName?? ""), 
                   new XAttribute("last-name", user.LastName?? ""),
                   new XElement("sold-products")
               );
               users.Add(newUser);

               foreach (Product product in user.products)
               {
                  newUser.Add(new XElement("Product",
                     new XElement("name", product.Name),
                     new XElement("price", product.Price)
                  ));
               }
            }
        }
    }
    public class cUser
    {
        public static List<cUser> users { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<Product> products { get; set; }
    }
    public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
}

答案 1 :(得分:0)

您可以通过引用sold-products

来执行此操作
foreach (var user in users)
{
    var soldProducts = new XElement("sold-products");

    xml.Add(new XElement("user",
        new XAttribute("first-name", user.FirstName?? ""),
        new XAttribute("last-name", user.LastName?? ""),
        soldProducts));

    foreach (var product in user.Products)
    {
        soldProducts.Add(new XElement("Product",
            new XElement("name", product.Name),
            new XElement("price", product.Price)));
    }
}

或者使用更具说明性的方法,它非常适合LINQ to XML:

var doc = new XDocument(
    new XElement("users",
        from user in users
        select new XElement("user",
            new XAttribute("first-name", user.FirstName ?? ""),
            new XAttribute("last-name", user.LastName ?? ""),
            new XElement("sold-products",
                from product in user.Products
                select new XElement("Product",
                    new XElement("name", product.Name),
                    new XElement("price", product.Price))))));