C# 在c中插入多个xml元素#

C# 在c中插入多个xml元素#,c#,asp.net,xml,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&g

我想创建包含多个元素的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>
如何解决此问题,以便正确获取数据?
提前感谢

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

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


也在这里检查,基本上是相同的问题。

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

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


也请检查这里,基本上是相同的问题。

您是否考虑过使用System.XML.Serialization命名空间,它有一个XMLSerializer类,可以很好地完成这类工作?这里有一些MSDN文档--其中深入介绍了一些好的示例,这里有一个简短的描述,足以说明基本内容--

您是否考虑过使用System.XML.Serialization名称空间,它有一个XMLSerializer类,可以很好地完成这类工作?这里有一些MSDN文档,其中有一些很好的例子,有一些很深入的说明,还有一个简短的说明,基本内容已经足够了-

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

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

你还没有说你正在使用哪个版本的.NET。如果使用.NET3.5或更高版本,那么我建议使用LINQtoXML,特别是当您可以从查询中获取强类型数据时。例如:

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

这也可能有帮助-这也可能有帮助-只是一个参考,但您不需要rdr.HasRows,因为它与rdr.Read()是冗余的,rdr.Read()只在找到并返回另一行时返回true。只是一个参考,但您不需要rdr.HasRows,因为它与rdr.Read()是冗余的,rdr.Read()只在找到并返回另一行时返回true。
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; }
    }
}