2010-11-30 4 views
0

내부에 여러 요소가있는 XML 문서를 만들고 싶습니다. 형식은 다음과 같이해야한다 :여러 xml 요소를 C로 삽입하십시오 #

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

모든 데이터를 가져 오는 첫 번째 루프 후 확인하지만, 2 차 루프가 완료되면,이 있습니다 : 그래서 모든 라운드 후

<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 필요하지 않습니다. – Zachary

답변

4

어디서 id, title 등의 노드를 만드나요? 새로운 노드를 만드는 대신에 이것을 재사용하는 것 같습니다. 이것이 올바르게 작동하지 않는 이유입니다.

자식 노드를 재사용하는 경우 현재 노드에서 제거하고 새 노드에 삽입하므로 빈 요소가 표시됩니다.

여기에서 this question도 확인하십시오. 기본적으로 똑같은 문제입니다.

0

이런 종류의 일을 잘 처리하는 XMLSerializer 클래스가있는 System.XML.Serialisation 네임 스페이스를 사용 해본 적이 있습니까? 여기에 몇 가지 MSDN 설명서가 있습니다 - http://support.microsoft.com/kb/815813

+0

이것은 도움이 될 수도 있습니다 - http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx – RichardW1001

+1

버전 중립적 인 링크 : http://msdn.microsoft.com/en-us /library/182eeyhh.aspx –

2

사용중인 .NET 버전을 언급하지 않았습니다. 여기에 몇 가지 좋은 예제가 들어있는 http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx이 있습니다. .NET 3.5 이상을 사용하는 경우 XML에서 LINQ를 사용하는 것이 좋습니다 (특히 쿼리에서 강력한 형식의 데이터를 얻을 수있는 경우). 예를 들어 그것을 발견하고 다른 행을 반환하는 경우에만 true를 돌려 rdr.Read()와 중복이기 때문에 그냥

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; } 
    } 
} 
관련 문제