471,570 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Constructing simple XML?

What is the best way to construct this type of XML

<ROOT><Person PersonId=\"1000\" /><Person PersonId=\"1001\" /><Person
PersonId=\"1002\" /></ROOT>

when I'm getting back only the three numerical values? The is occuring
in a loop and I'm getting a variable such as PersonID, which holds the
values. The XML needs to be a string but I'd like to see what a more
complex type would look like (XMLdoc type?).

Thanks,
Brett

Jan 25 '06 #1
5 3425
This pretty much covers the creation part:
http://www.csharphelp.com/archives/archive199.html. Still not sure how
to get the two backslashes in there.

Brett

Jan 25 '06 #2
Brett, you create attribute nodes just like element nodes. Here's the code
from the link you posted, modified to create the XML in your original post:

XmlDocument xmldoc;
XmlAttribute attr;
XmlElement elem;
XmlElement root;

xmldoc=new XmlDocument();

//let's add the XML declaration section
XmlNode xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclarati on,"","");
xmldoc.AppendChild(xmlnode);

//let's add the root element
elem=xmldoc.CreateElement("ROOT");
xmldoc.AppendChild(elem);
root = elem;

//add person elements. assume you have some data structure called ListOfIds
containing the numbers
foreach (int personId in ListOfIds)
{
elem = xmldoc.CreateElement("Person");
attr = xmldoc.CreateAttribute("PersonId");
attr.InnerText = personId.ToString();
elem.Attributes.Append(attr)
root.AppendChild(elem);
}

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
This pretty much covers the creation part:
http://www.csharphelp.com/archives/archive199.html. Still not sure how
to get the two backslashes in there.

Brett

Jan 26 '06 #3
Thanks.

If I try to read my file, which looks like this:
<?xml version="1.0"?>
<ROOT>
<Person PersonID="83615">
</Person>
</ROOT>

I get this error:
"The data at the root level is invalid. Line 1, position 1."

I'm using:
xmlDoc.LoadXml(@"C:\Documents and Settings\BROMERO\My
Documents\testing.spec");

I'm not sure if it is referring to the LoadXML method or the file. Any
ideas?

The <?xml version="1.0"?> part was generated with the XMLDeclaration
enumerator.

Thanks,
Brett

Jan 26 '06 #4
Hi Brett,
the LoadXML method assumes the string you are passing as a parameter is
XML, not a file path. If you want to load XML from a file use the
XmlDocument.Load method and pass in the file name.

Hope that helps
Mark Dawson
http://www.markdawson.org

"Brett Romero" wrote:
Thanks.

If I try to read my file, which looks like this:
<?xml version="1.0"?>
<ROOT>
<Person PersonID="83615">
</Person>
</ROOT>

I get this error:
"The data at the root level is invalid. Line 1, position 1."

I'm using:
xmlDoc.LoadXml(@"C:\Documents and Settings\BROMERO\My
Documents\testing.spec");

I'm not sure if it is referring to the LoadXML method or the file. Any
ideas?

The <?xml version="1.0"?> part was generated with the XMLDeclaration
enumerator.

Thanks,
Brett

Jan 26 '06 #5
For really quick XML construction, I suggest looking at the XmlWriter class
(and its descending implementations).

--
Venlig hilsen
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Jan 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Benjamin Scott | last post: by
1 post views Thread by Steven Arnold | last post: by
7 posts views Thread by Hyoung Lee | last post: by
10 posts views Thread by Matthew | last post: by
1 post views Thread by dmcdougald | last post: by
1 post views Thread by BigBaz | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

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.