2017-04-02 11 views
0

私は以下のコードを持ち、 "sell-product"要素を取得して製品を追加する方法を知りました。入れ子になったxml要素に追加する

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))); 
    } 
} 
+0

XDocumentに関する私の文書をチェックしてください。おそらくあなたの問題を解決することができます:http://stackoverflow.com/documentation/c%23/1528/xmldocument-and-the-system-xml-namespace#t=201704021228091722075 –

答えて

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; } 
    } 
} 
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とうまく本当にフィットし、より宣言的なアプローチを、使用しての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)))))); 
+0

私。 –

+0

@ M.Georgiev良いもの。どちらもうまくいくはずです。二番目のスタイルはちょうど異なるスタイルです。文書がどのように作成されたかの詳細につぶれているのではなく、文書を記述しています。 –

+0

私は2番目を試しましたが、私はいくつかの例外を投げます –

関連する問題