insert multiple xml elements in c#

I want to create XML document with multiple elements inside. The format should be something like this:

<Item>
  <Id>2276138</Id> 
  <Title>92907-03100-00 WASHER, CHAIN</Title> 
  <Link>http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00</Link> 
  <Price>0.0000</Price> 
  <Description>WASHER, CHAIN (92907-03100-00)</Description> 
  <Condition>New</Condition> 
  <Brand /> 
  <Product_Type /> 
  <Availability>In Stock</Availability> 
  <Manufacturer>Suzuki</Manufacturer> 
  </Item>

Everything is ok after the first loop of fetching data, but once the 2nd loop is done, I have this:

 <Item></Item>
    <Item>
      <Id>2276138</Id> 
      <Title>92907-03100-00 WASHER, CHAIN</Title> 
      <Link>http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00</Link> 
      <Price>0.0000</Price> 
      <Description>WASHER, CHAIN (92907-03100-00)</Description> 
      <Condition>New</Condition> 
      <Brand /> 
      <Product_Type /> 
      <Availability>In Stock</Availability> 
      <Manufacturer>Suzuki</Manufacturer> 
      </Item>

So after every round of fetching I get the empty item element and just the last element is populated. Here is the code for the loop:

 while (rdr.Read())
                {
                    if (rdr.HasRows)
                    {
                        XmlElement part = docOrders.CreateElement("Item");
                        id.InnerText = rdr[0].ToString();
                        part.AppendChild(id);
                        title.InnerText = rdr[1].ToString();
                        part.AppendChild(title);
                        link.InnerText = rdr[2].ToString();
                        part.AppendChild(link);
                        price.InnerText = rdr[3].ToString();
                        part.AppendChild(price);
                        desc.InnerText = rdr[4].ToString();
                        part.AppendChild(desc);
                        cond.InnerText = rdr[5].ToString();
                        part.AppendChild(cond);
                        brand.InnerText = rdr[6].ToString();
                        part.AppendChild(brand);
                        productType.InnerText = rdr[7].ToString();
                        part.AppendChild(productType);
                        availability.InnerText = rdr[8].ToString();
                        part.AppendChild(availability);
                        manufacturer.InnerText = rdr[9].ToString();
                        part.AppendChild(manufacturer);                        
                        root.AppendChild(part);
                    }
                }
                rdr.Close();
            }

How Can I solve this, so the data will be fetched correctly? Thanks in advance

Answers


where do you create the id, title, etc. nodes? It looks like you're reusing these instead of creating new nodes, that's why it's not working correctly.

If you're reusing a child node, it will remove it from the current node and insert it into the new node, that's why you're seeing the empty element.

Also check this question here on SO, basically the same exact problem.


Need Your Help

Java Help with Logic in XML SAX Handler

java xml parsing sax handler

I am trying to set up a SAX Handler to parse XML with the following structure:

Integrating external scripts with Zend Framework

php zend-framework

What is the best way to integrate an external script into the Zend Framework? Let me explain because I may be asking this the wrong way. I have a script that downloads and parses an XML file. This