Finding Descendents in XML



I have this problem with my C# API that I unfortunately posted incorrectly & got partially incorrect answers, so I'm going to try again since I've still not solved it (trying for ages)


Here's my XML file :



<HandlingData>
<Item type="CHandlingData">
<handlingName>Plane</handlingName>
<fMass value="140000.000000" />
<SubHandlingData>
<Item type="CFlyingHandlingData">
<handlingType>HANDLING_TYPE_FLYING</handlingType>
<fThrust value="0.630000" />
</SubHandlingData>
</Item>


Please note: SubHandlingData is in separate sub-section within the same item, a 2nd item type is called.


Here's the C# Code :



var items = doc.Descendants("HandlingData").Elements("Item");
var query = from i in items

select new {
HandlingName = (string)i.Element("handlingName"), (decimal?)i.Element("fThrust").Attribute("value"),
HandlingType = (string)i.Element("handlingType")
};
StringBuilder test = new StringBuilder();
foreach (var item in query)
{

var k = item.dunno;
test.Append(k);
richTextBox1.Text = test.ToString();

}


The above code works fine, but doesn't work with SubHandlingData. This means such values as HandlingType is not seen, I've added the element for this & it returns errors, have also tried including multiple decedents which then doesn't find HandlingName, or HandlingType. I want my output to be if handlingType == "HANDLING_TYPE_FLYING", richtextbox1.text = this.HandlingName. I hope I have explained this clearly enough to gain an answer as this has troubled me for ages.


My Problem in short: Program does not find HandlingType inside SubHandlingData of my XML document.


No comments:

Post a Comment