Laziale
Laziale

Reputation: 8225

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

Upvotes: 0

Views: 3261

Answers (3)

John Saunders
John Saunders

Reputation: 161773

You haven't said which version of .NET you are using. If using .NET 3.5 or above, then I would recommend using LINQ to XML, especially if you can get strongly-typed data from your query. For example:

using System;
using System.Linq;
using System.Xml.Linq;

public class Testing
{
    private void Main()
    {
        var items = new[]
                        {
                            new DataItem
                                {
                                    Id = 2276138,
                                    Title = "92907-03100-00 WASHER, CHAIN",
                                    Link =
                                        new Uri(
                                        "http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00"),
                                    Price = 0.0M,
                                    Description = "WASHER, CHAIN (92907-03100-00)",
                                    Condition = "New",
                                    Availability = "In Stock",
                                    Manufacturer = "Suzuki"
                                },
                            new DataItem
                                {
                                    Id = 2276139,
                                    Title = "92907-03100-01 WASHER, CHAIN",
                                    Link =
                                        new Uri(
                                        "http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00"),
                                    Price = 0.0M,
                                    Description = "WASHER, CHAIN (92907-03100-00)",
                                    Condition = "New",
                                    Availability = "In Stock",
                                    Manufacturer = "Suzuki"
                                },
                            new DataItem
                                {
                                    Id = 2276140,
                                    Title = "92907-03100-02 WASHER, CHAIN",
                                    Link =
                                        new Uri(
                                        "http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00"),
                                    Price = 0.0M,
                                    Description = "WASHER, CHAIN (92907-03100-00)",
                                    Condition = "New",
                                    Availability = "In Stock",
                                    Manufacturer = "Suzuki"
                                },
                        };
        var doc = new XDocument(
            new XElement(
                "Items",
                from item in items
                select
                    new XElement(
                    "Item",
                    new XElement("Id", item.Id),
                    new XElement("Title", item.Title),
                    new XElement("Link", item.Link),
                    new XElement("Price", item.Price),
                    new XElement("Description", item.Description),
                    new XElement("Condition", item.Condition),
                    new XElement("Brand", item.Brand),
                    new XElement("Product_Type", item.ProductType),
                    new XElement("Availability", item.Availability),
                    new XElement("Manufacturer", item.Manufacturer))));
    }

    public class DataItem
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public Uri Link { get; set; }
        public decimal Price { get; set; }
        public string Description { get; set; }
        public string Condition { get; set; }
        public string Brand { get; set; }
        public string ProductType { get; set; }
        public string Availability { get; set; }
        public string Manufacturer { get; set; }
    }
}

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160852

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.

Upvotes: 4

RichardW1001
RichardW1001

Reputation: 1985

Have you considered using the System.XML.Serialisation namespaces, which has an XMLSerializer class which does this kind of thing very well for you? There's some MSDN documentation here - http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx - which goes into depth with some good examples, and a shorter description here which has enough for the basics - http://support.microsoft.com/kb/815813

Upvotes: 0

Related Questions