如何使用c#创建xml [英] How to create xml using c#
本文介绍了如何使用c#创建xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i想要像这样创建XML
< 记录 >
< FIELD ID = 1 MAX_LENGTH = 100 / >
< FIELD ID = 2 MAX_LE NGTH = 100 / >
< FIELD ID = 3 MAX_LENGTH = 7 / >
< FIELD ID = 4 MAX_LENGTH = 100 / >
< FIELD ID = 5 MAX_LENGTH = 100 / >
< / RECORD >
< ROW >
< COLUMN SOURCE = 3 NAME = Col1 / >
< COLUMN SOURCE = 2 NAME = Col2 / >
< COLUMN SOURCE = 1 NAME = Col3 / >
< COLUMN SOURCE = 4 NAME = Col4 / >
< / ROW > ; 跨度>
这是我到目前为止所做的,但它在最后一行中的抛出错误为此文档已经有一个documentelement节点
XmlDocument xmlDoc = new XmlDocument();
XmlElement rootNode = xmlDoc.CreateElement( RECORD);
xmlDoc.AppendChild(rootNode);
int dbIndex = 0 ;
foreach (ColumnMapping columnMapping in columnMappings)
{
XmlNode userNode = xmlDoc.CreateElement( FIELD);
XmlAttribute attribute = xmlDoc.CreateAttribute( ID);
attribute.Value = dbIndex.ToString();
userNode.Attributes.Append(attribute);
rootNode.AppendChild(userNode);
dbIndex ++;
}
rootNode = null ;
rootNode = xmlDoc.CreateElement( ROW);
xmlDoc.AppendChild(rootNode);
解决方案
XmlDocument不允许这样做 - 它一次只能支持一个文档。您在顶部显示的示例是两个文档。将它们组合成一个文档(带有一个根节点),或者单独生成它们。
XML文档形成一个从根开始并分支到叶子的树结构。
XML文档必须包含一个根元素。这个元素是所有其他元素的父级。
XML文档中的元素构成一个文档树。树从根开始并分支到树的最低层。
- http://www.w3schools。 com / xml / xml_tree.asp [ ^ ]
您可以使用XmlSerializer
,XDocument
或XmlWriter
阅读这个主题以获取更多细节。它解释得非常好在c中创建xml#
也可以阅读 C#XmlWriter
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement( TABLE)); // DocumentElement
XmlElement rootNode = xmlDoc.CreateElement( RECORD);
xmlDoc.DocumentElement.AppendChild(rootNode); // DocumentElement
int dbIndex = 0 ;
foreach (ColumnMapping columnMapping in columnMappings)
{
XmlNode userNode = xmlDoc.CreateElement( FIELD);
XmlAttribute attribute = xmlDoc.CreateAttribute( ID);
attribute.Value = dbIndex.ToString();
userNode.Attributes.Append(attribute);
rootNode.AppendChild(userNode);
dbIndex ++;
}
rootNode = null ; // 不用
rootNode = xmlDoc.CreateElement( ROW);
xmlDoc.DocumentElement.AppendChild(rootNode); // DocumentElement
hi,
i want to create XML like this
<RECORD>
<FIELD ID="1"MAX_LENGTH="100" />
<FIELD ID="2" MAX_LENGTH="100" />
<FIELD ID="3" MAX_LENGTH="7"/>
<FIELD ID="4" MAX_LENGTH="100"/>
<FIELD ID="5" MAX_LENGTH="100"/>
</RECORD>
<ROW>
<COLUMN SOURCE="3" NAME="Col1" />
<COLUMN SOURCE="2" NAME="Col2" />
<COLUMN SOURCE="1" NAME="Col3" />
<COLUMN SOURCE="4" NAME="Col4" />
</ROW>
This is what i have done so far, but its throwing error in the last line as "This document already has a documentelement node"
XmlDocument xmlDoc = new XmlDocument();
XmlElement rootNode = xmlDoc.CreateElement("RECORD");
xmlDoc.AppendChild(rootNode);
int dbIndex = 0;
foreach (ColumnMapping columnMapping in columnMappings)
{
XmlNode userNode = xmlDoc.CreateElement("FIELD");
XmlAttribute attribute = xmlDoc.CreateAttribute("ID");
attribute.Value = dbIndex.ToString();
userNode.Attributes.Append(attribute);
rootNode.AppendChild(userNode);
dbIndex++;
}
rootNode = null;
rootNode = xmlDoc.CreateElement("ROW");
xmlDoc.AppendChild(rootNode);
解决方案
XmlDocument won't allow that -- it can only support one document at a time. The example you show at the top is two documents. Either combine them into one document (with one root node), or produce them separately.
"
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
XML documents must contain a root element. This element is "the parent" of all other elements.
The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.
"
-- http://www.w3schools.com/xml/xml_tree.asp[^]
You can useXmlSerializer
,XDocument
orXmlWriter
Read this thread for more details.It explains very well about creating xml in c#
Read C# XmlWriter too
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.AppendChild(xmlDoc.CreateElement("TABLE")); // DocumentElement XmlElement rootNode = xmlDoc.CreateElement("RECORD"); xmlDoc.DocumentElement.AppendChild(rootNode); // DocumentElement int dbIndex = 0; foreach (ColumnMapping columnMapping in columnMappings) { XmlNode userNode = xmlDoc.CreateElement("FIELD"); XmlAttribute attribute = xmlDoc.CreateAttribute("ID"); attribute.Value = dbIndex.ToString(); userNode.Attributes.Append(attribute); rootNode.AppendChild(userNode); dbIndex++; } rootNode = null; // needless rootNode = xmlDoc.CreateElement("ROW"); xmlDoc.DocumentElement.AppendChild(rootNode); // DocumentElement
这篇关于如何使用c#创建xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文