title: Can I use variable to fill xml with multiple values in ASP.Net c#?
body: <p>Hello everyone I am trying to create and save XML with values from SQL Server database.</p>
  
  <p>My code basically creates an XML string filled with the variables that I get from several SQL queries (I need to fetch data from many tables to complete the XML)</p>
  
  <p>I am using <code>SqlDataReader</code> to fetch values for my variables.</p>
  
  <p>Since my XML will be kind of long and complicated, I decided to put the final XML strings from a few sub-strings according to the queries, basically like:</p>
  
  <pre><code>finalxml string = startxml + headerxml + buyerxml + sellerxml + linesxml + summaryxml
  </code></pre>
  
  <p>I was all OK till I reached a point, where a SQL query returned more than one row as a result.</p>
  
  <p>Here is my code-behind:</p>
  
  <pre><code>string orderdetails = "select ProductId, Quantity from [Order] inner join OrderItem on [Order].Id=OrderItem.OrderId where OrderId='" + orderID + "'";
  SqlCommand com3 = new SqlCommand(orderdetails, dbConn);
  
  dbConn.Open();
  
  using (SqlDataReader orderitem = com3.ExecuteReader())
      while (orderitem.Read())
      {
              string orderxml = "&lt;Order&gt;" + "&lt;OrderItem&gt;" + orderitem.GetInt32(0).ToString() + "&lt;/OrderItem&gt;" + "&lt;OrderItem&gt;" + orderitem.GetInt32(1).ToString() + "&lt;/OrderItem&gt;" + "&lt;/Order&gt;";
              Label20.Text = orderxml;
              string termid = orderitem.GetInt32(0).ToString();
              string termmenny = orderitem.GetInt32(1).ToString();
      }
  
      dbConn.Close();
  }
  
  protected void Button1_Click(object sender, EventArgs e)
  {
      string headerxml = "&lt;Order-Header&gt;" +
                              "&lt;DocumentType&gt; számla &lt;/DocumentType&gt;" +
                              "&lt;OrderNumber&gt;" + rendSz + "&lt;/OrderNumber&gt;" +   
                              "&lt;OrderDate&gt;"+ datum +"&lt;/OrderDate&gt;" +   
                              "&lt;ExpectedDeliverydate&gt; " + szallido + "&lt;/ExpectedDeliverydate&gt;"+
                              "&lt;PaymentMethod&gt;"+ fizmod +"&lt;/PaymentMethod&gt;"+
                              "&lt;Remarks&gt;&lt;![CDATA[" + txtFirstName.Text + "]]&gt;&lt;/Remarks&gt;"+
                              "&lt;Note&gt;&lt;![CDATA[" + txtLastName + "]]&gt;&lt;/Note&gt;" +   
                              "&lt;PreviousOrderNumber&gt;&lt;![CDATA[]]&gt;&lt;/PreviousOrderNumber&gt;" +
                          "&lt;/Order-Header&gt;";
      string vevoxml = "&lt;Buyer&gt;" +
                                  "&lt;ILN&gt;435&lt;/ILN&gt;" +
                                      "&lt;Name&gt;"+ vezeteknev +" "+ keresztnev +"&lt;/Name&gt;" +
                                      "&lt;City&gt;" + vevovaros + "&lt;/City&gt;" +
                                      "&lt;ZIP&gt;" + irszam + "&lt;/ZIP&gt;" +
                                      "&lt;Address&gt;" + vevocim1 + vevocim2 + "&lt;/Address&gt;" +
                                      "&lt;E-mail&gt;" + vevoemail +  "&lt;/E-mail&gt;" +
                                      "&lt;Telefon&gt;" + vevotel + "&lt;/Telefon&gt;" +
                                      "&lt;Contact&gt;" + "nincs" + "&lt;/Contact&gt;" +
                                      "&lt;DeliveryName&gt;" + szallkeresztnev +" "+ szallvezeteknev + "&lt;/DeliveryName&gt;" +
                                      "&lt;DeliveryCity&gt;" + szallcimvaros + "&lt;/DeliveryCity&gt;" +
                                      "&lt;DeliveryZIP&gt;" + szallcimirszam + "&lt;/DeliveryZIP&gt;" +
                                      "&lt;DeliveryAddress&gt;" + szallvevocim1 + szallvevocim2 + "&lt;/DeliveryAddress&gt;" +
                                  "&lt;/Buyer&gt;";
  
      string eladoxml = "&lt;Seller&gt;" +
                                      "&lt;ILN /&gt;" +
                                      "&lt;Name&gt;E-Szoftver Kft.&lt;/Name&gt;" +
                                      "&lt;City&gt;Budapest&lt;/City&gt;" +
                                      "&lt;ZIP&gt;1195&lt;/ZIP&gt;" +
                                      "&lt;Address&gt;Ady Endre út 97-99. F/04.&lt;/Address&gt;" +
                                      "&lt;E-mail&gt;laszlo@marsalsoft.hu&lt;/E-mail&gt;" +
                                  "&lt;/Seller&gt;";
  
      string linexml = "&lt;Line&gt;" +
                                 "&lt;Line-Item&gt;" +
                                      "&lt;LineNumber&gt;1&lt;/LineNumber&gt;" +
                                      "&lt;EAN /&gt;" +
                                      "&lt;SupplierItemCode /&gt;" +
                                      "&lt;CustomsCode&gt;5829&lt;/CustomsCode&gt;" +
                                      "&lt;ItemDescription&gt;&lt;![CDATA[E-Számla szoftver frissítési és jogkövetési díj 1 évre]]&gt;&lt;/ItemDescription&gt;" +
                                      "&lt;ItemNote&gt;&lt;![CDATA[]]&gt;&lt;/ItemNote&gt;" +
                                      "&lt;VATType&gt;27&lt;/VATType&gt;" +
                                      "&lt;PackageType&gt;CU&lt;/PackageType&gt;" +
                                      "&lt;OrderedQuantity&gt;1&lt;/OrderedQuantity&gt;" +
                                      "&lt;UnitOfMeasure&gt;év&lt;/UnitOfMeasure&gt;" +
                              "&lt;OrderedUnitNetPrice&gt;13200&lt;/OrderedUnitNetPrice&gt;" +
                                  "&lt;/Line-Item&gt;" +
                                  "&lt;/Line&gt;"; 
  
  
      string strMyXml = "&lt;?xml version=\"1.0\" encoding=\"windows-1250\"?&gt; " +
                        "&lt;Document-Order&gt;"+
                          headerxml +
  
                              "&lt;Order-Parties&gt;" +
                                  vevoxml +
                                  eladoxml +
                              "&lt;/Order-Parties&gt;"+
                              "&lt;Order-Lines&gt;" +
                                  linexml +
                              "&lt;/Order-Lines&gt;" +
                              "&lt;Order-Summary&gt;" +
                                  "&lt;TotalLines&gt;1&lt;/TotalLines&gt;" +
                                  "&lt;TotalOrderedAmount&gt;1&lt;/TotalOrderedAmount&gt;" +
                                  "&lt;TotalNetPrice&gt;13200&lt;/TotalNetPrice&gt;" +
                                  "&lt;TotalVat&gt;3564&lt;/TotalVat&gt;" +
                                  "&lt;TotalGross&gt;16764&lt;/TotalGross&gt;" +
                              "&lt;/Order-Summary&gt;" +
                       "&lt;/Document-Order&gt;";
  
      XmlDocument xDoc = new XmlDocument();
      xDoc.LoadXml(strMyXml);
  
      xDoc.Save(Server.MapPath("//orders//szamla.xml"));
  }
  </code></pre>
  
  <p>Now the <code>lineXML</code> part is my problem, it describes the products, that is included in the order. Every xml describes 1 order, but 1 order can contain 3 products, and in that case I need to create a line for every product with a few elements: number, quantity, net price, gross price.</p>
  
  <p>Is it possible with <code>SqlDataReader</code> and variable added to the string, or do I need to use some other way, like dataset generated from the query results? </p>
  
  <p>I found a lot of info about generating xml from dataset, and generating xml with string, but none of them contained info about this particular situation, generating XML from SQL Server where you need to use multiple queries.</p>
  
  <p>Luckily my code is modular, so if it can be done with dataset only, I don't need to touch the other parts of the xml hopefully. Any help would be appreciated.</p>