Reading XML using System.Xml.XmlReader

  • Thread starter Thread starter TOMERDR
  • Start date Start date
T

TOMERDR

Hi,

I have the following xml and i need to read all elements and insert
them to a hash table

hash[businessaddresscity]=""
hash[email1address]="(e-mail address removed)"

it seems that the reader is skeeping the password element


string x="<?xml version='1.0'?>" +
"<Document>" +
"<businessaddresscity />"+
"<email1address>[email protected]</email1address>" +
"<department />" +
"<password>{a18505d9-45bd-4772-87f5-3c24d786bd1d}</password>" +
"<companyname />" +
"<businessfaxnumber />" +
"<businessaddresspostalcode />"+
"</Document>";



public void ReadXml(System.Xml.XmlReader reader)
{
while (reader.Read())
{
if (reader.IsStartElement())
{
string propertyName= reader.Name;
reader.Read(); //Read the start tag.
this[propertyName.ToLower()]=reader.ReadString();
}
}
}
 
This is just from reading your code so I might be a bit off, but I
think this is correct.

What happens is that the first call to Read moves the XmlReader to the
xml node. Then the IsStartElement moves the reader to the Document tag.
The ReadStartElement call then moves the reader to the
businessaddresscity element. So the first time the loop is run you
store the key value pair of <"Document", "">, where the empty string is
the value of the businessaddresscity. So the password element isn't
skipped, you just read past it and store the value of it with
department as key.

I think you need to read the docs for the XmlReader a bit more.

/Johan
 

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