XML Assistance needed

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I need to store customers and their emails in an XML file for quick lookup
in a small program. I am kind of a bit confused with this XML thing. Do I use
XML or this XSD Schema?

I came up with this

<customers>
<customer>
<id>1</id>
<email>[email protected]</email>
<email>[email protected]</email>
</customer>
<customer>
<id>2</id>
<email>[email protected]</email>
</customer>
<customer>
<id>3</id>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
</customer>
</customers>

Is this useable? If yes, where do I get information on reading and writing
to XML? I couldn't find any information either on searching, ex if I want to
get all the email addresses for a specific customer (id).

Thanks
 
Hi,
You can do it easily by using XMLDocument and xpath query.
Below link shows how to read xml file and also how to add nodes to it.
http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c9427__1/
And below link shows how to update nodes in xml file:
http://www.dotnetspider.com/kb/Article2938.aspx.
Also i would suggest you to changes structure of your xml file to the
following:
<customers>
<customer id="1">
<email>[email protected]</email>
<email>[email protected]</email>
</customer>
<customer id="2">
<email>[email protected]</email>
</customer>
<customer id="3">
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
</customer>
</customers>
If you are having trouble using xpath query to retreive all the email
addresses for a specific customer (id) then please do let me know
 
Hi,
How do I find all emails for a specific id? I was trying the xpath query

"customers/customer[@id=2]" but that doesn't work.

Thanks
 
Hi,
Below code is working on my machine:
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlNodeList nodelist =
doc.SelectNodes("//customers/customer[@id='2']//email");

for(int i= 0;i<nodelist.Count;i++)
{
XmlNode node = nodelist;
MessageBox.Show(node.InnerText.ToString());
}
--
Hope this answers your question.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



Chris said:
Hi,
How do I find all emails for a specific id? I was trying the xpath query

"customers/customer[@id=2]" but that doesn't work.

Thanks

Manish Bafna said:
Hi,
You can do it easily by using XMLDocument and xpath query.
Below link shows how to read xml file and also how to add nodes to it.
http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c9427__1/
And below link shows how to update nodes in xml file:
http://www.dotnetspider.com/kb/Article2938.aspx.
Also i would suggest you to changes structure of your xml file to the
following:
<customers>
<customer id="1">
<email>[email protected]</email>
<email>[email protected]</email>
</customer>
<customer id="2">
<email>[email protected]</email>
</customer>
<customer id="3">
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
</customer>
</customers>
If you are having trouble using xpath query to retreive all the email
addresses for a specific customer (id) then please do let me know
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 

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

Back
Top