【求助】【急】XML节点的简单设置

sunboywo 2009-03-25 09:25:40
我要输出的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);

请大家指教!谢谢
...全文
155 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
the_pain 2009-03-26
  • 打赏
  • 举报
回复
up
sunboywo 2009-03-25
  • 打赏
  • 举报
回复
谢谢了,问题解决!
CutBug 2009-03-25
  • 打赏
  • 举报
回复
 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();
}
scy251147 2009-03-25
  • 打赏
  • 举报
回复
sunboywo 2009-03-25
  • 打赏
  • 举报
回复
自己顶。。。
cppfaq 2009-03-25
  • 打赏
  • 举报
回复
<?xml version="1.0" standalone="yes"?>
<TMapping xmlns="http://tempuri.org/TMapping.xsd"> --DataSet的名称和xml命名空间
<TMappings> --DataTable的名称
<ID>1</ID> --列名和行值
</TMappings>
</TMapping>
cppfaq 2009-03-25
  • 打赏
  • 举报
回复

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>

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧