Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从XML导入数据集后,新行未正确嵌套_C#_Xml_System.data - Fatal编程技术网

C# 从XML导入数据集后,新行未正确嵌套

C# 从XML导入数据集后,新行未正确嵌套,c#,xml,system.data,C#,Xml,System.data,我正在使用DataSet.ReadXml()将XML文件导入到新的数据集中。然后我将向数据集中的一个表中添加一个新行,然后我想再次将该数据集导出为XML。问题是新行没有正确嵌套,只是被追加到XML文件的末尾 节目如下: 使用系统; 使用系统数据; 使用System.IO; 使用System.Xml; 公共课程 { 公共静态void Main() { 字符串xml=@“ "; XmlReader=XmlReader.Create(新的StringReader(xml)); 数据集=新数据集();

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

节目如下:

使用系统;
使用系统数据;
使用System.IO;
使用System.Xml;
公共课程
{
公共静态void Main()
{
字符串xml=@“
";
XmlReader=XmlReader.Create(新的StringReader(xml));
数据集=新数据集();
ReadXml(reader,XmlReadMode.InferTypedSchema);
var rowTable=dataSet.Tables[“行”];
var newRow=rowTable.newRow();
新行[“公司ID”]=“应用程序”;
newRow[“Description”]=“Apple”;
rowTable.Rows.Add(newRow);
WriteLine(dataSet.GetXml());
}
}
以下是输出:


我希望新行与该表中的其他行嵌套,如下所示:


我做错了什么? 如何从
DataSet.GetXml()
中获取格式良好的XML


ReadXml将xml分割成许多表。ReadXml使用以下嵌套标记执行以下操作1)数据集名称2)数据表名称3)行数据:列名为标记,innertext为值

请参阅下面我的代码,它使用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());
        }
    }
}
使用系统;
使用系统数据;
使用System.IO;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
公共静态void Main()
{
字符串xml=@“
";
XmlReader=XmlReader.Create(新的StringReader(xml));
数据集=新数据集();
ReadXml(reader,XmlReadMode.InferTypedSchema);
var rowTable=dataSet.Tables[“行”];
var newRow=rowTable.newRow();
新行[“公司ID”]=“应用程序”;
newRow[“Description”]=“Apple”;
rowTable.Rows.Add(newRow);
WriteLine(dataSet.GetXml());
XDocument doc=XDocument.Parse(xml);
DataTable rowTable2=新的DataTable(“Table1”);
DataRow newRow2=null;
foreach(文档子体中的XElement字段(“字段”))
{
字符串t=(字符串)field.Attribute(“fieldtype”);
类型_Type=null;
开关(t)
{
大小写“字符串”:
_类型=类型(字符串);
打破
}
rowTable2.Columns.Add((string)field.Attribute(“attrname”),_type);
}
foreach(文档子体中的XElement行(“行”))
{
newRow=rowTable2.Rows.Add();
foreach(row.Attributes()中的XAttribute属性)
{
newRow[attribute.Name.LocalName]=(字符串)属性;
}
}
newRow=rowTable2.Rows.Add();
新行[“公司ID”]=“应用程序”;
newRow[“Description”]=“Apple”;
数据集ds=新数据集();
ds.Tables.Add(表2);
WriteLine(ds.GetXml());
}
}
}

你从哪里得到的XML?它的格式不受
数据集的支持。嵌套表时,必须定义表之间的父子关系,并且必须将子表的
Nested
属性设置为
true
。在XML中,
数据集
不知道新的子行属于哪个父行,因此它会将其追加到末尾

您可以在MSDN中阅读关于

话虽如此,XML实际上并没有父表和子表。它有
元数据
行数据
。正如我所说,
DataSet
不支持这种格式,您必须将元数据移动到
模式(XSD)。您可以在MSDN中阅读关于

下面是一个使用XSD和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""?>
<MyDataSet>
    <Companies>
        <CompanyID>CC</CompanyID>
        <Description>Contoso</Description>
    </Companies>
</MyDataSet>
";
        string xsd = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <xs:schema id=""SomeID""
            xmlns=""""
            xmlns:xs=""http://www.w3.org/2001/XMLSchema""
            xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
   <xs:element name=""MyDataSet"" msdata:IsDataSet=""true"">
     <xs:complexType>
       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
         <xs:element name=""Companies"">
           <xs:complexType >
             <xs:sequence>
               <xs:element name=""CompanyID"" type=""xs:string"" minOccurs=""0"" />
               <xs:element name=""Description"" type=""xs:string"" minOccurs=""0"" />  
             </xs:sequence>
           </xs:complexType>
          </xs:element>
       </xs:choice>
     </xs:complexType>
   </xs:element>
 </xs:schema>
";
        DataSet dataSet = new DataSet();
        StringReader sr = new StringReader(xsd);
        dataSet.ReadXmlSchema(sr);
        sr = new StringReader(xml);
        dataSet.ReadXml(sr, XmlReadMode.InferTypedSchema);
        var rowTable = dataSet.Tables["Companies"];
        var newRow = rowTable.NewRow();
        newRow["CompanyID"] = "APPL";
        newRow["Description"] = "Apple";
        rowTable.Rows.Add(newRow);
        Console.WriteLine(dataSet.GetXml());
    }
}
使用系统;
使用系统数据;
使用System.IO;
使用System.Xml;
公共课程
{
公共静态void Main()
{
字符串xml=@“
科科斯群岛
康托索
";
字符串xsd=@”
";
数据集=新数据集();
StringReader sr=新的StringReader(xsd);
ReadXmlSchema(sr);
sr=新的StringReader(xml);
ReadXml(sr,XmlReadMode.InferTypedSchema);
var rowTable=dataSet.Tables[“公司”];
var newRow=rowTable.newRow();
新行[“公司ID”]=“应用程序”;
newRow[“Description”]=“Apple”;
rowTable.Rows.Add(newRow);
WriteLine(dataSet.GetXml());
}
}
在这种情况下,您实际上不需要架构,因为您只有一个表,其中所有列都是
string
。因此,如果您从上面的代码中删除模式并再次运行,您将得到完全相同的结果。但是,这使您了解了如何使用模式定义
数据集
结构,以便添加