I would like to place a follow-up to a previously asked question but apparently I'm not allowed to continue this in the same thread.
In my previous question (Iterating through a list of elements inside an element with a specific index) I was looking for the value of all of the urls in my xml. But what is the proper way to access different values inside the same node? An example:
<list index="1" name="" title="" id="5702f74a-9df2-12e5-89e0-f947f6dd0a1f">
<theme>
<properties>
<field index="1"/>
<field index="2" name="title"></field>
</properties>
<list>
<item id="f391fada-90c5-f239-a836-25ca76311286">
<field index="1">
<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>
<title></title>
</field>
<field index="2" name="question">This is a question</field>
<field index="3" name="answer">This is an answer</field>
<field index="4" name="remark"/>
<field index="5" name="reveal"/>
</item>
<item id="a0b97163-d195-4dce-b970-ecb6eb080403">
<field index="1">
<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>
<title></title>
</field>
<field index="2" name="question"/>
<field index="3" name="answer"></field>
<field index="4" name="remark"/>
<field index="5" name="reveal"/>
</item>
</list>
</theme>
</list>
I would like to read this XML piece by piece (item by item in the XML) and store the values of the fields with specific names into variables: So for example:
string url = [value of field URL];
string question= [value of field with name question];
string answer= [value of field with name answer];
string remark= [value of field with name remark];
string reveal= [value of field with name reveal];
Do I have to use for every variable a sortlike query?
var string = xmlDoc.Descendants("list")
.Where(e => (int)e.Attribute("index") == 1)
.Descendants("item").Descendants("field")
.Where(e => (string)e.Attribute("index") == "1")
.Select(e => (string)e.Value());
Or is there a way to get the fields in a sort of list which I can then search by it's indexnumber?
foreach (field in fields)
{
switch case (field.name)
{
case "question":
question = field.value;
break;
case "answer":
answer= field.value;
break;
}
}
Any ideas are greatly appreciated.
Try this. The URL field doesn't contain a name or index tag so you have to test for name tag not equal null.