Reading Xml using XmlReader
-
Hi folks, I am a bit puzzled how to use the System.Xml.XmlReader to read data :doh: To give a simple example I want to read a Xml file like this: Peter Alen in a object with the same properties... what I did was something like this: ... Person person = new Person(); while (reader.Read()) { if (reader.Name=="Name") { person.Name = reader.ReadElementContentAsString(); } if (reader.Sirname == "Sirname") { person.Sirname = reader.ReadElementContentAsString(); } } ... of course simplified, the real thing is a bit more complicated... this does work if the Xml is exactly the same way I outlined above but if, for example, the Name tag comes after the Sirname tag the code doesn't work anymore because the reader.ReadElementContentAsString() is advancing the reader and the reader.Read() as well thus one element is simply not handled by the code in the while loop... I hope I explained it well enough :^) any suggestions welcome! TIA Pakl
-
Hi folks, I am a bit puzzled how to use the System.Xml.XmlReader to read data :doh: To give a simple example I want to read a Xml file like this: Peter Alen in a object with the same properties... what I did was something like this: ... Person person = new Person(); while (reader.Read()) { if (reader.Name=="Name") { person.Name = reader.ReadElementContentAsString(); } if (reader.Sirname == "Sirname") { person.Sirname = reader.ReadElementContentAsString(); } } ... of course simplified, the real thing is a bit more complicated... this does work if the Xml is exactly the same way I outlined above but if, for example, the Name tag comes after the Sirname tag the code doesn't work anymore because the reader.ReadElementContentAsString() is advancing the reader and the reader.Read() as well thus one element is simply not handled by the code in the while loop... I hope I explained it well enough :^) any suggestions welcome! TIA Pakl
If you don't have a proper schema, you're better off using an XmlDocument or XmlDataDocument, and XPath
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
If you don't have a proper schema, you're better off using an XmlDocument or XmlDataDocument, and XPath
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
hmm, alright... then I will switch to XPath for reading and use XmlWriter for writing... thanks just another thought... I searched for any trick I could use to keep using XmlReader... maybe something like this...
bool read = false;
do
{
read = false;
if (reader.Name=="Person")
{
person = reader.ReadElementContentAsString();
read = true;
}
..... other stuff comes here...
}
while (read || reader.Read())but of course I don't want to create a mess when I forget to set the read = true.... doesn't seem to fit so I will use XPath...
-
hmm, alright... then I will switch to XPath for reading and use XmlWriter for writing... thanks just another thought... I searched for any trick I could use to keep using XmlReader... maybe something like this...
bool read = false;
do
{
read = false;
if (reader.Name=="Person")
{
person = reader.ReadElementContentAsString();
read = true;
}
..... other stuff comes here...
}
while (read || reader.Read())but of course I don't want to create a mess when I forget to set the read = true.... doesn't seem to fit so I will use XPath...
The core issue is that you don't have a defined schema. It would be better if you did.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
The core issue is that you don't have a defined schema. It would be better if you did.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
I understand. Just a question on that topic. If I have a defined schema, does this mean that those tags have to be in the same order all the time. I always thought that it is 'correct' to place tags in any order in the file.
-
I understand. Just a question on that topic. If I have a defined schema, does this mean that those tags have to be in the same order all the time. I always thought that it is 'correct' to place tags in any order in the file.
No, a schema defines the order as well.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
No, a schema defines the order as well.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
hmm, alright, probably I am not understanding that so well after all :doh: how exactly would it help me, if I had a schema... btw. thanks for your time
-
hmm, alright, probably I am not understanding that so well after all :doh: how exactly would it help me, if I had a schema... btw. thanks for your time
It wouldn't, directly, unless you needed to validate the documents coming in. But, it's very uncommon for a program to not care what order the nodes are in, or expect them to differ.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert