473,383 Members | 1,868 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,383 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 3461
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Benjamin Scott | last post by:
Hello. I attempted to build a compound dictionary: len(Lst)=1000 len(nuerLst)=250 len(nuestLst)=500 Dict={}
1
by: Steven Arnold | last post by:
Is there a more elegant way to construct \ in a string than something like: s = '\\n' result = eval( "'%s'" ) % s Another ugly method would be to build a dict with all the different special...
7
by: Hyoung Lee | last post by:
A simple method of simulating a class, such as C++ or UML class, in C would be using the struct construct. Similarly, we use C functions to simulate a methods; e.g., rv method_a (&struct,...
4
by: Sagaert Johan | last post by:
i get this error if i write this : using System.Net.Sockets; .... .... TcpClient tcp; tcp=new TcpClient();
10
by: Matthew | last post by:
I found an excellent tutorial on XML Serialization: http://addressof.com/blog/articles/ExploringXMLSerialization.aspx When he creates a class property, it looks like this: Public Class Drivers...
11
by: bgreen | last post by:
I am hoping for some assistance with formatting a large text file which consists of a series of individual records. Each record includes specific labels/field names (a sample of 1 record (one of...
26
by: drako | last post by:
Hi, I'm a bit stumped as I am getting a "Notice: Array to String Conversion" error when trying to do something that on the surface should be a very simple task - create an array, and write a set...
1
by: dmcdougald | last post by:
I have scenario where I need to create an object of an unknown type. Reference types work fine. I call Type.GetConstructors(), let the user pick a ConstructorInfo and then call...
2
by: PeterAlt | last post by:
Hello all! This is my first post here. I've been going crazy lately trying to master the art of working with and creating my own objects with Javascript. I see the HUGE power potential of object...
1
by: BigBaz | last post by:
If we have a graph G=(N,E), where N is the set of nodes, and E is the set of edges. If we partition N into k parts (partition 1, 2,3...k). And this partition is given by an array, with the node...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.