Help, I am XML stupid

  • Thread starter Thread starter Gina_Marano
  • Start date Start date
G

Gina_Marano

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~
 
Gina_Marano said:
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>
 
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~
 
Hi,

Gina_Marano said:
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 ;-)

HTH,
Laurent
 
Thanks Laurent!

~Gina~
Hi,

Gina_Marano said:
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 ;-)

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
 
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);
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 + " 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~
 
Gina_Marano said:
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);
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 + " 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;
}
}
 
Hey Tom,

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

Graciously,

~Gina~

Tom said:
Gina_Marano said:
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);
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 + " 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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top