473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Create XMLDocument on the fly.

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.

Nov 16 '05 #1
4 15957
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?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"aaa" <so*****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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.


Nov 16 '05 #2
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" <so*****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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-----
Nov 16 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
aaa
Thnx!

"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:#i**************@TK2MSFTNGP10.phx.gbl...
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" <so*****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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-----

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Spiegel | last post by:
Hi all, I'm trying to efficiently pull data out of an xml file into a XmlDocument AND create another "sub" document based on one subtree of the document. For example, say I've got: <Books>...
5
by: david | last post by:
In my code, I use this code to create a xml string and then load it into a xmldocument. for(int i=0;i<sqlReader.FieldCount;i++){ ...
1
by: Peter Nofelt | last post by:
Hey All, I'm running into this issue with parsing through an xml document by tag name. Below is an example xml document: File Name: things.xml <things> <people> <name>Peter</name>
0
by: AlexAdam | last post by:
I have been trying to find a sample coding in C# of how to create the XmlDocument based on its XSD schema fixed or default element values. I could accomplisht this work somehow like this: (but...
1
by: David | last post by:
I need to find a good online resource which teaches the use of the XmlDocument framework in more depth than is covered in MS's online doc. I need to create a multi-level XML document like the one...
0
by: David | last post by:
Hi, how should I create XmlDocument and depend schema from a entity ..xsd file? And how to insert some value into this XmlDocument? Thanks, David.
0
by: Luis Esteban Valencia | last post by:
Hello. I got this message <ns0:Orden xmlns:ns0="http://localhost/CreditCheck"> <IdCliente>10</IdCliente> - <Pedido> <IdProducto>IdProducto_0</IdProducto> <Cantidad>10.4</Cantidad>...
2
by: ojinfo | last post by:
hi i am currently working on a web service which is supposed to take a xmldocument as input parameter, and then returns another xmldocument. the service is added to the web references in my...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.