Create XMLDocument on the fly.

A

aaa

Can someone show me a snippet that creates a very simple XML doc with either
a root and chilc nodes or root child nodes, and attributes.

I have looked all around and every example I try I get an error of either:

1. Cannot set a value on node type: Element.
2. Specified argument was out of the range of valid values.
3. This document already has a DocumentElement node.

Here is the code i've been working with:
XmlDocument xDoc= new XmlDocument();
XmlDeclaration xmlDecl = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xDoc.AppendChild(xmlDecl);
XmlAttribute nameAttr;
int f = 3;

for (int i = 0; i <= f; i ++)
{
XmlNode topNode = xDoc.CreateNode(XmlNodeType.Element, "blah", null);

nameAttr = xDoc.CreateAttribute(null, "a", null);
nameAttr.Value = "123";
topNode.Attributes.Append(nameAttr);
nameAttr = xDoc.CreateAttribute(null, "b", null);
nameAttr.Value = "123";
topNode.Attributes.Append(nameAttr);
nameAttr = xDoc.CreateAttribute(null, "c", null);
nameAttr.Value = "123";
topNode.Attributes.Append(nameAttr);
nameAttr = xDoc.CreateAttribute(null, "d", null);
nameAttr.Value = "123";
topNode.Attributes.Append(nameAttr);
nameAttr = xDoc.CreateAttribute(null, "e", null);
nameAttr.Value = "123";
topNode.Attributes.Append(nameAttr);
}

I have tried all different node types and no luck. thnx.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,
1. Cannot set a value on node type: Element.

Use the InnerText property instead.
2. Specified argument was out of the range of valid values.

On which line does this error happen?
3. This document already has a DocumentElement node.

Looks like you are trying to create multiple top-level nodes, which is
prohibited. I can't see from your code where you append the created nodes to
xDoc - again, can you specify the exact line where this error happens?
 
R

Ron Allen

A moderate sized example. I've left out a long section of code that pulls
information from a DataTable and puts it into 'page' nodes. This is pulled
from a working report generation function. Note that I've turned the code
that creates a text element into a subroutine call.

public XmlDocument TimeSumDoc()
{
int result = 0;
if ((result = LoadTimeData(m_start, m_end)) < 0)
{
throw new ArgumentOutOfRangeException("Period or Personnel", "No charge
data for period.");
}
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null));
XmlElement root = doc.CreateElement("TimeSummary");
XmlElement dateTime = doc.CreateElement("DateTime");
dateTime.AppendChild(XmlTextNode(doc, "date",
DateTime.Now.ToShortDateString()));
dateTime.AppendChild(XmlTextNode(doc, "time",
DateTime.Now.ToShortTimeString()));
root.AppendChild(dateTime);
root.AppendChild(XmlTextNode(doc, "perStart",
m_start.ToShortDateString()));
root.AppendChild(XmlTextNode(doc, "perEnd", m_end.ToShortDateString()));
XmlElement page = null;
{
// a lot of code to form one or more page nodes appending each as it
goes
}
root.AppendChild(page); // append the last one
}
doc.AppendChild(root);
return doc;
}
/// <summary>
/// Creates an xml element node with the specified name and text
/// </summary>
/// <param name="doc">associated document</param>
/// <param name="nodeName">node name to use</param>
/// <param name="nodeText">node text</param>
/// <returns></returns>
private XmlElement XmlTextNode(XmlDocument doc, string nodeName, string
nodeText)
{
XmlElement result = doc.CreateElement(nodeName);
result.AppendChild(doc.CreateTextNode(nodeText));
return result;
}

Ron Allen
aaa said:
Can someone show me a snippet that creates a very simple XML doc with
either
a root and chilc nodes or root child nodes, and attributes.
---- snip-----
 
W

WizyDig

Where is the data coming from. if it's comming from a database you can
use the xml writer built into the dataset to create your xml.

mydataset.GetXml();

That is very easy

also you can get the schema with
mydataset.GetXmlSchema();

Wiz
 
A

aaa

Thnx!

Ron Allen said:
A moderate sized example. I've left out a long section of code that pulls
information from a DataTable and puts it into 'page' nodes. This is pulled
from a working report generation function. Note that I've turned the code
that creates a text element into a subroutine call.

public XmlDocument TimeSumDoc()
{
int result = 0;
if ((result = LoadTimeData(m_start, m_end)) < 0)
{
throw new ArgumentOutOfRangeException("Period or Personnel", "No charge
data for period.");
}
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null));
XmlElement root = doc.CreateElement("TimeSummary");
XmlElement dateTime = doc.CreateElement("DateTime");
dateTime.AppendChild(XmlTextNode(doc, "date",
DateTime.Now.ToShortDateString()));
dateTime.AppendChild(XmlTextNode(doc, "time",
DateTime.Now.ToShortTimeString()));
root.AppendChild(dateTime);
root.AppendChild(XmlTextNode(doc, "perStart",
m_start.ToShortDateString()));
root.AppendChild(XmlTextNode(doc, "perEnd", m_end.ToShortDateString()));
XmlElement page = null;
{
// a lot of code to form one or more page nodes appending each as it
goes
}
root.AppendChild(page); // append the last one
}
doc.AppendChild(root);
return doc;
}
/// <summary>
/// Creates an xml element node with the specified name and text
/// </summary>
/// <param name="doc">associated document</param>
/// <param name="nodeName">node name to use</param>
/// <param name="nodeText">node text</param>
/// <returns></returns>
private XmlElement XmlTextNode(XmlDocument doc, string nodeName, string
nodeText)
{
XmlElement result = doc.CreateElement(nodeName);
result.AppendChild(doc.CreateTextNode(nodeText));
return result;
}

Ron Allen

---- snip-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top