我要输出的xml格式为
<?xml version="1.0" standalone="yes"?>
<TMapping xmlns="http://tempuri.org/TMapping.xsd">
  <TMappings>
    <ID>1</ID>
  <TMappings>
<TMapping>我现在输出的是
<?xml version="1.0" standalone="yes"?>
<DocumentElement xmlns="http://tempuri.org/TMapping.xsd">
  <TMappings>
    <ID>1</ID>
  <TMappings>
<TMapping>
我现在的代码是ToTable为datatable类型:              Row["ID"] = "1";
              ToTable.Rows.Add(Row);
            ToTable.DataSet.DataSetName = "qq";//报错
            ToTable.Prefix = "";
            ToTable.Namespace = "http://tempuri.org/TMapping.xsd";
            ToTable.TableName = "TMappings";
            ToTable.WriteXml(SFilename);请大家指教!谢谢

解决方案 »

  1.   


    static void Main(string[] args)
            {
                DataSet ds = new DataSet();
                DataTable ToTable = new DataTable();
                ds.Tables.Add(ToTable);
                ToTable.Columns.Add("ID");
                DataRow Row = ToTable.NewRow();
                Row["ID"] = "1";
                ToTable.Rows.Add(Row);
                ToTable.DataSet.DataSetName = "TMapping";//报错 
                ToTable.Prefix = "";
                ds.Namespace = "http://tempuri.org/TMapping.xsd";
    //            ToTable.Namespace = "http://tempuri.org/TMapping.xsd";
                ToTable.TableName = "TMappings";
                ToTable.WriteXml("test.xml"); 
            }
    生成:
    <?xml version="1.0" standalone="yes"?>
    <TMapping xmlns="http://tempuri.org/TMapping.xsd">
      <TMappings>
        <ID>1</ID>
      </TMappings>
    </TMapping>
      

  2.   

    <?xml version="1.0" standalone="yes"?>
    <TMapping xmlns="http://tempuri.org/TMapping.xsd">  --DataSet的名称和xml命名空间
      <TMappings>                                       --DataTable的名称
        <ID>1</ID>                                      --列名和行值
      </TMappings>
    </TMapping>
      

  3.   

     static void Main(string[] args)
            {
                DataTable ToTable = new DataTable();
                ToTable.Columns.Add(new DataColumn("ID", typeof(string)));
                ToTable.Rows.Add("1");
                ToTable.Prefix = "";
                ToTable.Namespace = "http://tempuri.org/TMapping.xsd";
                ToTable.TableName = "TMappings";
                Write(ToTable, @"E:\ConsoleApplication1\test.xml", "TMapping");
            }        static void Write(DataTable table, string fileName, string StartElementName)
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.CheckCharacters = true;
                settings.CloseOutput = true;
                settings.ConformanceLevel = ConformanceLevel.Document;
                settings.Encoding = System.Text.Encoding.UTF8;
                settings.Indent = false;
                settings.NewLineHandling = NewLineHandling.Replace;
                settings.NewLineOnAttributes = true;
                settings.OmitXmlDeclaration = false;            using (XmlWriter writer = XmlWriter.Create(fileName, settings))
                {
                    writer.WriteStartDocument(true);
                    WriteTable(writer, table, StartElementName);
                    writer.WriteEndDocument();
                }        }        static void WriteTable(XmlWriter writer, DataTable table,string StartElementName)
            {
                writer.WriteStartElement(StartElementName);            foreach (DataRow row in table.Rows)
                {
                    writer.WriteStartElement(table.TableName);                foreach (DataColumn column in table.Columns)
                    {
                        writer.WriteStartElement(column.ColumnName);
                        writer.WriteValue(row[column].ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();            }
                writer.WriteEndElement();
            }