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

Worklight 6.1 - Host is not responsive. Android login issue

android worklight worklight-server

I am getting the below error while trying login to Android device from my locally deployed code. Both the machine &amp; device uses the same WIFI connection(router) . Code is working as expected in...

Wide angle lenses calibration with Opencv

opencv camera computer-vision camera-calibration calibration

I'm using a wide-angle lens (178º Diagonal FOV ) and I'm trying to calibrate it properly using Opencv Calibration module. All the detection and calibration process are working fine, but the result is

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.