首页 > 解决方案 > 从 XML 导入 DataSet 后,新行未正确嵌套

问题描述

我正在使用DataSet.ReadXml()将 XML 文件导入新的数据集。然后我在 DataSet 中的一个表中添加一个新行,然后我想再次将该 DataSet 导出到 XML。问题是新行没有正确嵌套,只是被附加到 XML 文件的末尾。

这是程序:

    using System;
    using System.Data;
    using System.IO;
    using System.Xml;

    public class Program
    {
        public static void Main()
        {
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
    <DATAPACKET Version=""2.0"">
        <METADATA>
            <FIELDS>
                <FIELD attrname=""CompanyID"" fieldtype=""string"" WIDTH=""10""/>
                <FIELD attrname=""Description"" fieldtype=""string"" WIDTH=""40""/>
            </FIELDS>
            <PARAMS/>
        </METADATA>
        <ROWDATA>
            <ROW CompanyID=""CC"" Description=""Contoso""/>
        </ROWDATA>
    </DATAPACKET>
    ";
            XmlReader reader = XmlReader.Create(new StringReader(xml));
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(reader, XmlReadMode.InferTypedSchema);
            var rowTable = dataSet.Tables["ROW"];
            var newRow = rowTable.NewRow();
            newRow["CompanyID"] = "APPL";
            newRow["Description"] = "Apple";
            rowTable.Rows.Add(newRow);
            Console.WriteLine(dataSet.GetXml());
        }
    }

这是输出:

    <DATAPACKET Version="2.0">
      <METADATA>
        <PARAMS />
        <FIELDS>
          <FIELD attrname="CompanyID" fieldtype="string" WIDTH="10" />
          <FIELD attrname="Description" fieldtype="string" WIDTH="40" />
        </FIELDS>
      </METADATA>
      <ROWDATA>
        <ROW CompanyID="CC" Description="Contoso" />
      </ROWDATA>
    </DATAPACKET>
    <ROW CompanyID="APPL" Description="Apple" />

我想要的是新行与该表中的其他行嵌套,如下所示:

    <DATAPACKET Version="2.0">
      <METADATA>
        <PARAMS />
        <FIELDS>
          <FIELD attrname="CompanyID" fieldtype="string" WIDTH="10" />
          <FIELD attrname="Description" fieldtype="string" WIDTH="40" />
        </FIELDS>
      </METADATA>
      <ROWDATA>
        <ROW CompanyID="CC" Description="Contoso" />
        <ROW CompanyID="APPL" Description="Apple" />
      </ROWDATA>
    </DATAPACKET>

我究竟做错了什么?我如何得到格式良好的 XML 出来DataSet.GetXml()

这是在 dotnetfiddle 运行的程序

标签: c#xmlsystem.data

解决方案


ReadXml 将您的 xml 分成许多表。ReadXml 使用以下嵌套标签 1) 数据集名称 2) 数据表名称 3) 行数据:列名是标签,内文是值

请参阅下面的代码,该代码使用 xml linq 解析 xml:

using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
    <DATAPACKET Version=""2.0"">
        <METADATA>
            <FIELDS>
                <FIELD attrname=""CompanyID"" fieldtype=""string"" WIDTH=""10""/>
                <FIELD attrname=""Description"" fieldtype=""string"" WIDTH=""40""/>
            </FIELDS>
            <PARAMS/>
        </METADATA>
        <ROWDATA>
            <ROW CompanyID=""CC"" Description=""Contoso""/>
        </ROWDATA>
    </DATAPACKET>
    ";

            XmlReader reader = XmlReader.Create(new StringReader(xml));
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(reader, XmlReadMode.InferTypedSchema);
            var rowTable = dataSet.Tables["ROW"];
            var newRow = rowTable.NewRow();
            newRow["CompanyID"] = "APPL";
            newRow["Description"] = "Apple";
            rowTable.Rows.Add(newRow);
            Console.WriteLine(dataSet.GetXml());


            XDocument doc = XDocument.Parse(xml);

            DataTable rowTable2 = new DataTable("Table1");
            DataRow newRow2 = null;
            foreach (XElement field in doc.Descendants("FIELD"))
            {
                string t = (string)field.Attribute("fieldtype");
                Type _type = null;
                switch (t)
                {
                    case "string" :
                        _type = typeof(string);
                        break;
                }

                rowTable2.Columns.Add((string)field.Attribute("attrname"), _type);
            }
            foreach (XElement row in doc.Descendants("ROW"))
            {
                newRow = rowTable2.Rows.Add();
                foreach (XAttribute attribute in row.Attributes())
                {
                    newRow[attribute.Name.LocalName] = (string)attribute;
                }
            }
            newRow = rowTable2.Rows.Add();
            newRow["CompanyID"] = "APPL";
            newRow["Description"] = "Apple";
            DataSet ds = new DataSet();
            ds.Tables.Add(rowTable2);
            Console.WriteLine(ds.GetXml());
        }
    }
}

推荐阅读