在c#中插入多个xml元素

时间:2010-11-30 21:09:00

标签: c# asp.net xml

我想创建包含多个元素的XML文档。格式应该是这样的:

<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>

在第一次获取数据循环后一切正常,但是一旦第二次循环完成,我就有了这个:

 <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>

因此,在每轮抓取之后,我得到空的item元素,并且只填充最后一个元素。 这是循环的代码:

 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();
            }

如何解决此问题,以便正确获取数据? 提前致谢

3 个答案:

答案 0 :(得分:4)

你在哪里创建id,title等节点?看起来您正在重复使用这些而不是创建新节点,这就是它无法正常工作的原因。

如果您正在重用子节点,它会将其从当前节点中删除并将其插入新节点,这就是您看到空元素的原因。

同时在SO上检查this question,基本上是完全相同的问题。

答案 1 :(得分:2)

您还没有说过您使用的是哪个版本的.NET。如果使用.NET 3.5或更高版本,那么我建议使用LINQ to XML,特别是如果您可以从查询中获取强类型数据。例如:

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; }
    }
}

答案 2 :(得分:0)

您是否考虑过使用System.XML.Serialisation命名空间,该命名空间有一个XMLSerializer类,可以很好地为您完成此类操作?这里有一些MSDN文档 - http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx - 深入介绍了一些很好的例子,这里有一个简短的描述,其中有足够的基础知识 - http://support.microsoft.com/kb/815813