I have a problem with my C# API not finding element decimals (returning error) Here's the XML file
<CHandlingDataMgr>
<HandlingData>
<Item type="CHandlingData">
<handlingName>Plane</handlingName>
<fMass value="140000.000000" />
<SubHandlingData>
<Item type="CFlyingHandlingData">
<handlingType>HANDLING_TYPE_FLYING</handlingType>
<fThrust value="0.630000" />
</Item>
</Item>
</SubHandlingData>
</HandlingData>
I'm aiming to show the handlingName & fMass value into my RichTextBox
Here's the C# code :
string path = "Data//handling4.meta";
var doc = XDocument.Load(path);
var items = doc.Descendants("HandlingData").Elements("Item");
var query = from i in items
select new
{
HandlingName = (string)i.Element("handlingName"),
HandlingType = (string)i.Element("SubHandlingData")
.Element("Item")
.Element("handlingType")//.Attribute("value").Value
};
StringBuilder test = new StringBuilder();
foreach (var item in query)
{
string k = item.HandlingName + item.HandlingType ;
test.Append(k);
richTextBox1.Text = test.ToString();
}
The above code works perfectly, providing the handling name & handling type, my problem however, is the thrust values because the thrust value is decimal, however subhandlingdata is a string(tag), so my question is how to find a decimal inside of a string's element?
I have tried placing (decimal?)
inside many different sections of .Element("fThrust").Value
with no luck. If using (decimal?)i.Element("fThrust").Attribute("value")
it returns 'not set to object' because this method doesn't look inside sub-item (subHandlingData)
Thanks in advance
You need to be selecting the fThrust
element within the inner item, not the outer one - just as you're already doing for HandlingType
:
Thrust = (decimal?) i.Element("SubHandlingData")
.Element("Item")
.Elements("fThrust") // There may not be an element
.Attributes("value")
.FirstOrDefault()
See more on this question at Stackoverflow