PC Review


Reply
Thread Tools Rate Thread

Create XMLDocument on the fly.

 
 
aaa
Guest
Posts: n/a
 
      14th Feb 2005
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.



 
Reply With Quote
 
 
 
 
Dmitriy Lapshin [C# / .NET MVP]
Guest
Posts: n/a
 
      15th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>
>
>


 
Reply With Quote
 
Ron Allen
Guest
Posts: n/a
 
      16th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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-----


 
Reply With Quote
 
WizyDig
Guest
Posts: n/a
 
      16th Feb 2005
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!
 
Reply With Quote
 
aaa
Guest
Posts: n/a
 
      17th Feb 2005
Thnx!

"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:#(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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-----
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XmlSerializer.Serialize, XmlWriter.Create and XmlDocument working together? Andy B Microsoft C# .NET 1 9th Apr 2008 07:09 AM
create XmlDocument depend a xsd file David Microsoft C# .NET 0 26th Apr 2005 10:08 AM
Create XmlDocument from subset of another John Spiegel Microsoft Dot NET 0 1st Mar 2005 11:06 PM
How can I create an XMLDocument from SqlCommand.ExecuteXmlReader =?Utf-8?B?RGF2ZSBCb2Fs?= Microsoft Dot NET Framework 2 23rd Sep 2004 10:33 PM
Can an XmlNode from one XmlDocument be copied to another XmlDocument? Bob Microsoft ASP .NET 3 22nd Jul 2004 12:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:40 AM.