2016-05-27 6 views
1

Ich versuche, die unter XMLNested XML-Knoten Schlange nicht richtig

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<Application> 
    <organisation> 
     <organisation_id>0</organisation_id> 
    </organisation> 
    <address> 
     <address1>12345</address1> 
     <address2>qqqqq</address2> 
     <address3>ddddd</address3> 
    </address> 
    <Customer> 
     <custID>652</custID> 
     <address1>12345</address1> 
     <references> 
      <f_ref>456789</f_ref> 

      <licenses> 
       <id>3654</id> 
       <image>\photo\123.jpg</image> 
      </licenses> 

     </references> 

     <type> 
      <sort>1</sort> 
      <date>12/12/1997</date> 
     </type> 
     <internal> 
      <auto>true</auto> 
      <deliver>true</deliver> 
     </internal> 
    </Customer> 
</Application> 

für die ich den Code unten habe Grund zu produzieren:

MyDocument MyDoc = new MyDocument(
     new XDeclaration("1.0", "utf-8", "no")); 
     XElement MyRoot = new XElement("Application", 
       new XElement("organisation", 
        new XElement("organisation_id", "0")), 
        new XElement("address", 
         new XElement("address1", "123456"), 
         new XElement("address2", "qqqqq"), 
         new XElement("address3", "ddddd"))); 
     MyDoc.Add(MyRoot); 

     foreach (var c in GetCustomers()) 
     { 
      XElement Customers = new XElement("Customer", 
          new XElement("custID", "652"), 
             new XElement("address1", "12345")), 
              new XElement("references", 
              new XElement("f_ref", "456789"))); 
      MyRoot.Add(Customers); 

      foreach (License l in c.Licenses) 
      { 
       XElement Licenses = new XElement("licenses", 
         new XElement("id", "3654"), 
         new XElement("image", "\photo\123.jpg")); 
       MyRoot.Add(Licenses); 
      } 


      XElement Type = new XElement("type", 
          new XElement("sort", "1"), 
          new XElement("date", "12/12/1997")); 
      MyRoot.Add(Type); 

      XElement Internal = new XElement("internal", 
       new XElement("auto", "true"), 
       new XElement("deliver", "true")); 
      MyRoot.Add(Internal); 
     } 

die

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<Application> 
    <organisation> 
    <organisation_id>0</organisation_id> 
    </organisation> 
    <address> 
    <address1>12345</address1> 
    <address2>qqqqq</address2> 
    <address3>ddddd</address3> 
    </address> 
    <Customer> 
    <custID>652</custID> 
    <address1>12345</address1> 
    <references> 
     <f_ref>456789</f_ref> 
    </references> 
    </Customer> 
    <licenses> 
    <id>3654</id> 
    <image>\photo\123.jpg</image> 
    </licenses> 
    <type> 
    <sort>1</sort> 
    <date>12/12/1997</date> 
    </type> 
    <internal> 
    <auto>true</auto> 
    <deliver>true</deliver> 
    </internal> 
</Application> 
die unten stehende XML erzeugt

Ich habe die MyRoot.Add Methode umgestellt, sogar versucht, die foreach-Schleife innerhalb der XElement hinzuzufügen (was mir einen Syntaxfehler gibt) aber ich bin nicht sicher, wie man das XML im nachher produziert?

+0

Änderung von: MyRoot.Add (Internal); An: Customers.Add (Intern); – jdweng

Antwort

0

MyRoot ist Ihr Stamm Application Element. Dort fügen Sie licences, type und internal hinzu, so dass sie geschrieben werden.

Sie sollten sie zu customers stattdessen hinzufügen, verwenden Sie so Customers.Add für type und internal. licences sollte innerhalb des references Konstruktors enthalten sein - obwohl es möglicherweise einfacher ist, die Erstellung von references auf ähnliche Weise zu type und internal herauszuziehen.

So, nach ein bisschen neu zu ordnen den Code der Kompilierung zu machen:

var doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "no")); 

var root = new XElement("Application", 
    new XElement("organisation", 
     new XElement("organisation_id", "0")), 
    new XElement("address", 
     new XElement("address1", "123456"), 
     new XElement("address2", "qqqqq"), 
     new XElement("address3", "ddddd"))); 

doc.Add(root); 

var customers = new XElement("Customer", 
    new XElement("custID", "652"), 
    new XElement("address1", "12345") 
    ); 

root.Add(customers); 

var references = new XElement("references", 
    new XElement("f_ref", "456789")); 

var licenses = new XElement("licenses", 
    new XElement("id", "3654"), 
    new XElement("image", @"\photo\123.jpg")); 

references.Add(licenses); 

var type = new XElement("type", 
    new XElement("sort", "1"), 
    new XElement("date", "12/12/1997")); 

customers.Add(type); 

var @internal = new XElement("internal", 
    new XElement("auto", "true"), 
    new XElement("deliver", "true")); 

customers.Add(@internal); 

Siehe this fiddle für eine funktionierende Demo.