473,513 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help, I am XML stupid

Hey guys,

I am used to text files and such but thought I better get modern.

I want to store/read the following (example) in an xml file:

A list of DVDs each person has:

Amy
On Golden Pond
Harry Met Sally
Sleepless in Seattle

Sam
Cars
Poohs Adventure

Lisa
Speed
Speed 2
Bond
Notebook

....

I don't even know how i would structure it. I do NOT want to use any
schema or anything.

~Gina~

Jan 4 '07 #1
7 1237
Gina_Marano wrote:
Hey guys,

I am used to text files and such but thought I better get modern.

I want to store/read the following (example) in an xml file:

A list of DVDs each person has:

Amy
On Golden Pond
Harry Met Sally
Sleepless in Seattle

Sam
Cars
Poohs Adventure

Lisa
Speed
Speed 2
Bond
Notebook

...

I don't even know how i would structure it. I do NOT want to use any
schema or anything.
Schema can be used to define the valid structure for the XML, so why don't
you want to use schema?

Here is one simple layout, there are many possibilities:
<people>
<person>
<name>Amy</name>
<dvds>
<dvd>
<title>On Golden Pond</title>
</dvd>
<dvd>
<title>Harry Met Sally</title>
</dvd>
</dvds>
</person>
<person>
<name>Sam</name>
<dvds>
<dvd>
<title>Cars</title>
</dvd>
<dvd>
<title>Poohs Adventure</title>
</dvd>
</dvds>
</person>
</people>
--
Tom Porterfield

Jan 4 '07 #2
Thanks for the structure Tom!

I don't want to worry about the schema since I just need to store
simple lists. Basically I am just replacing an ini file.

Best class to use in attempting this?

~Gina~

Tom Porterfield wrote:
Gina_Marano wrote:
Hey guys,

I am used to text files and such but thought I better get modern.

I want to store/read the following (example) in an xml file:

A list of DVDs each person has:

Amy
On Golden Pond
Harry Met Sally
Sleepless in Seattle

Sam
Cars
Poohs Adventure

Lisa
Speed
Speed 2
Bond
Notebook

...

I don't even know how i would structure it. I do NOT want to use any
schema or anything.

Schema can be used to define the valid structure for the XML, so why don't
you want to use schema?

Here is one simple layout, there are many possibilities:
<people>
<person>
<name>Amy</name>
<dvds>
<dvd>
<title>On Golden Pond</title>
</dvd>
<dvd>
<title>Harry Met Sally</title>
</dvd>
</dvds>
</person>
<person>
<name>Sam</name>
<dvds>
<dvd>
<title>Cars</title>
</dvd>
<dvd>
<title>Poohs Adventure</title>
</dvd>
</dvds>
</person>
</people>
--
Tom Porterfield
Jan 4 '07 #3
Hi,

Gina_Marano wrote:
Thanks for the structure Tom!

I don't want to worry about the schema since I just need to store
simple lists. Basically I am just replacing an ini file.
A schema has a huge advantage: If you specify one, and enter data in
your XML file in Visual Studio (or another XML editor), the data is
automatically validated, and you get Intellisense support. That really
speeds up data entry.

But take one step after the other. Start with simple XML files, and then
move to XSD schemas.
Best class to use in attempting this?
The System.Xml namespace contains all classes needed to work with XML.
There are a few different ways to read XML data. I personally like the
XmlDocument class, because it reminds me of HTML and DOM Level 2 ;-)
>
~Gina~
HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 4 '07 #4
Thanks Laurent!

~Gina~

Laurent Bugnion [MVP] wrote:
Hi,

Gina_Marano wrote:
Thanks for the structure Tom!

I don't want to worry about the schema since I just need to store
simple lists. Basically I am just replacing an ini file.

A schema has a huge advantage: If you specify one, and enter data in
your XML file in Visual Studio (or another XML editor), the data is
automatically validated, and you get Intellisense support. That really
speeds up data entry.

But take one step after the other. Start with simple XML files, and then
move to XSD schemas.
Best class to use in attempting this?

The System.Xml namespace contains all classes needed to work with XML.
There are a few different ways to read XML data. I personally like the
XmlDocument class, because it reminds me of HTML and DOM Level 2 ;-)

~Gina~

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 4 '07 #5
Hey Guys,

OK, I can write to the xml file just fine:

private void WriteDVDListToFile()
{
string sFilename = "c:\\DVDList.xml";
string[] arrNames = new string[3] {"Maggie","Amy","Sam"};

Xmltw tw = new Xmltw(sFilename, null);
tw.WriteStartDocument();

tw.WriteStartElement("people");
for (int i = 0; i < arrNames.Length; i++)
{
//write the person's name
tw.WriteStartElement("person");
tw.WriteStartElement("name");
tw.WriteString(arrNames[i]);
tw.WriteEndElement();

tw.WriteStartElement("dvds");

//write each person' dvd titles
for (int j = 0; j <= 3; j++)
{
tw.WriteStartElement("dvd");
tw.WriteStartElement("title");
tw.WriteString(arrNames[i] + " title " + j.ToString());
tw.WriteEndElement(); //title
tw.WriteEndElement(); //dvd
}
tw.WriteEndElement(); //dvds
tw.WriteEndElement(); //person
}
tw.WriteEndElement(); //people
tw.WriteEndDocument();
tw.Close();
}

It matches the structure that was laid out above.

But reading it back is another thing... Am I making this too hard?

How do I get the name and then the list of DVDs dynamically?

Can I get a list of names then read the dvds for each name?

~Gina~

Jan 5 '07 #6
Gina_Marano wrote:
Hey Guys,

OK, I can write to the xml file just fine:

private void WriteDVDListToFile()
{
string sFilename = "c:\\DVDList.xml";
string[] arrNames = new string[3] {"Maggie","Amy","Sam"};

Xmltw tw = new Xmltw(sFilename, null);
tw.WriteStartDocument();

tw.WriteStartElement("people");
for (int i = 0; i < arrNames.Length; i++)
{
//write the person's name
tw.WriteStartElement("person");
tw.WriteStartElement("name");
tw.WriteString(arrNames[i]);
tw.WriteEndElement();

tw.WriteStartElement("dvds");

//write each person' dvd titles
for (int j = 0; j <= 3; j++)
{
tw.WriteStartElement("dvd");
tw.WriteStartElement("title");
tw.WriteString(arrNames[i] + " title " + j.ToString());
tw.WriteEndElement(); //title
tw.WriteEndElement(); //dvd
}
tw.WriteEndElement(); //dvds
tw.WriteEndElement(); //person
}
tw.WriteEndElement(); //people
tw.WriteEndDocument();
tw.Close();
}

It matches the structure that was laid out above.

But reading it back is another thing... Am I making this too hard?

How do I get the name and then the list of DVDs dynamically?

Can I get a list of names then read the dvds for each name?
A little XPath will get you going. For use an XmlDocument to load the data
back in, then use xpath queries to select nodes and get their text. As
follows:

XmlDocument xd = new XmlDocument();
xd.Load("c:\\DVDList.xml");

// get all the people
XmlNodeList people = xd.SelectNodes("people/person");
foreach (XmlNode person in people)
{
// get their name
string name = person.SelectSingleNode("name").InnerText;
// now get their dvds
XmlNodeList dvds = person.SelectNodes("dvds/dvd");
foreach (XmlNode dvd in dvds)
{
// get the title from each dvd node
string title = dvd.SelectSingleNode("title").InnerText;
}
}

--
Tom Porterfield

Jan 5 '07 #7
Hey Tom,

Thanks so much. I was almost done but your help really helpme finish it
up!

Graciously,

~Gina~

Tom Porterfield wrote:
Gina_Marano wrote:
Hey Guys,

OK, I can write to the xml file just fine:

private void WriteDVDListToFile()
{
string sFilename = "c:\\DVDList.xml";
string[] arrNames = new string[3] {"Maggie","Amy","Sam"};

Xmltw tw = new Xmltw(sFilename, null);
tw.WriteStartDocument();

tw.WriteStartElement("people");
for (int i = 0; i < arrNames.Length; i++)
{
//write the person's name
tw.WriteStartElement("person");
tw.WriteStartElement("name");
tw.WriteString(arrNames[i]);
tw.WriteEndElement();

tw.WriteStartElement("dvds");

//write each person' dvd titles
for (int j = 0; j <= 3; j++)
{
tw.WriteStartElement("dvd");
tw.WriteStartElement("title");
tw.WriteString(arrNames[i] + " title " + j.ToString());
tw.WriteEndElement(); //title
tw.WriteEndElement(); //dvd
}
tw.WriteEndElement(); //dvds
tw.WriteEndElement(); //person
}
tw.WriteEndElement(); //people
tw.WriteEndDocument();
tw.Close();
}

It matches the structure that was laid out above.

But reading it back is another thing... Am I making this too hard?

How do I get the name and then the list of DVDs dynamically?

Can I get a list of names then read the dvds for each name?

A little XPath will get you going. For use an XmlDocument to load the data
back in, then use xpath queries to select nodes and get their text. As
follows:

XmlDocument xd = new XmlDocument();
xd.Load("c:\\DVDList.xml");

// get all the people
XmlNodeList people = xd.SelectNodes("people/person");
foreach (XmlNode person in people)
{
// get their name
string name = person.SelectSingleNode("name").InnerText;
// now get their dvds
XmlNodeList dvds = person.SelectNodes("dvds/dvd");
foreach (XmlNode dvd in dvds)
{
// get the title from each dvd node
string title = dvd.SelectSingleNode("title").InnerText;
}
}

--
Tom Porterfield
Jan 6 '07 #8

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

Similar topics

7
2293
by: Ben Thomas | last post by:
Hi all, I'm having some trouble understanding the behavior of std::ostringstream. (I'm using Visual Studio .Net & STL port 4.5.3). I'll appreciate if someone can give me a little explanation of this behavior and how it is possible... Here's my code ////////////////////////// #include <stdio.h>
7
1738
by: Martin Feuersteiner | last post by:
Hi! I would be grateful for any advise regarding what I'm doing wrong.. My brain is stuck. Probably some stupid simple mistake I don't see. Thanks very much for your efforts! Martin I have this code: DECLARE @ContactID varchar(10),
4
2688
by: Jane Withnolastname | last post by:
I am trying to re-work an old site by replacing the html with css. On the main page, I have a logo image which I needed centred on the initial screen. I found the solution here: http://www.wpdfd.com/editorial/wpd0103.htm#toptip (the second example) The problem is, under the image is a large table. But using the above positioning, now the...
2
2291
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int word=0; char *querystring; querystring=malloc(sizeof(char)*100000); if (getenv("QUERY_STRING")==NULL)
17
5804
by: Yuri CHUANG | last post by:
Hi, I'm the beginner of the CPL.I write a program about the problem of Ring of Josephus,using DoubleLinkList data structure. I'm very confused that I think there is really no error in my code.But,the compiler sometimes show some strange errors,sometimes can pass through with an unknown trouble when it is running,and no result.So,could anyone...
66
5279
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of...
10
3329
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably the worst I ever seen. I almost cannot find anything I need, including things I
62
2660
by: vubaboota | last post by:
I HAVE SOME SERIOUS PROBLEM , HOW TO MAKE A PROGRAM IN C Q1: Write a program using malloc function. In which you take input from user and allocate memory equal to square of this number. Which multiply numbers and draw a table in the following format? Hint: User enters 3 then program allocates equal to 9 integer memories. Output:1
2
858
by: Aaron Brady | last post by:
Hendrik van Rooyen wrote: My manager doesn't know a thing about programming, and in fact he failed the intro to critical thinking course at his college. He's scared of truth tables. He's bored and picks fights. He spent my bonus on a new VGA monitor for his second office. When I want to change my syntax colors, he has to get permission...
12
2429
by: =?Utf-8?B?ZGdvdw==?= | last post by:
I designed a "contact_us" page in visual web developer 2005 express along with EW2 after viewing tutorials on asp.net's help page. Features work like they should, but I cannot figure out how to send contact info to email or data base when the "send" button is pressed. I've watched the tutorials over & over again. I just can't get it. Link:...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.