Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i want to create XML like this

XML
<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"

C#
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);
Posted
Updated 28-Oct-14 5:43am
v2
Comments
PIEBALDconsult 28-Oct-14 12:02pm    
What do you intend to do with it? Just hold it in memory? Store it somewhere?

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[^]
 
Share this answer
 
v4
Comments
Arjun Menon U.K 28-Oct-14 11:57am    
shall i create two docs and combine them and assign to another? Can you provide a sample
PIEBALDconsult 28-Oct-14 12:01pm    
You would still have to provide one root node for the combined document.
CPallini 28-Oct-14 12:20pm    
5.
Arjun Menon U.K 28-Oct-14 12:54pm    
Hi thanks for the elaborate detailing.
So if i want to create an XML like this dynamically.. How can i do this?
PIEBALDconsult 28-Oct-14 13:01pm    
You can do it in a string as far as I'm concerned, but you can't do it with a class that expects to contain an XML document and will verify the well-formedness of what you put in it.
What are you going to do with it? If you just want to write it to a file, you can use an XmlWriter as Dilan says, but you still won't be able to read it into an XmlDocument or XDocument.
Your best bet is to create one document and make those two documents parts of it:
<TABLE><RECORD...><ROW...></TABLE>
You can use XmlSerializer , XDocument or XmlWriter

Read this thread for more details.It explains very well about creating xml in c#

Read C# XmlWriter too
 
Share this answer
 
v2
Comments
PIEBALDconsult 28-Oct-14 12:35pm    
XmlWriter can do it only if you use System.Xml.ConformanceLevel.Fragment -- because it's not a document.
C#
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
 
Share this answer
 
Comments
Arjun Menon U.K 28-Oct-14 13:18pm    
Hi thanks a lot. I will try it out and get back to you ... :)
Arjun Menon U.K 28-Oct-14 13:34pm    
Worked like a charm... Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900