2010-11-30 72 views
0

我想创建包含多个元素的XML文档。格式应该是这样的:在c中插入多个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> 

每一轮之后,所以提取我得到空的项目元素,只是最后一个元素被填充。 下面是循环代码:

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

我怎样才能解决这个问题,这样的数据将被正确获取? 在此先感谢

+0

只是一个参考,但您不需要rdr.HasRows,因为它与rdr.Read()是多余的,它只在发现并返回另一行时返回true。 – Zachary 2010-11-30 21:59:09

回答

4

你在哪里创建ID,标题等节点?看起来你正在重复使用这些而不是创建新节点,这就是为什么它不能正常工作。

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

此外检查this question在这里,基本上相同的确切问题。

0

你有没有考虑过使用System.XML.Serialisation命名空间,它有一个XMLSerializer类,它对你来说很适合做这种事情?这里有一些MSDN文档 - http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx - 深入一些很好的例子,以及这里有一个简短的说明足够的基础知识 - http://support.microsoft.com/kb/815813

+0

这也可能有所帮助 - http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx – RichardW1001 2010-11-30 21:24:23

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