Danny Nichols
Danny Nichols

Reputation: 75

XML full file reading C#

So I have a code that reads partially into an XML document, precenting me with the first block of results which is great, but I have a file containing multiple blocks of the same code & my program seems to quit after the first.

Here's the code:

string path = "data//handling.meta";
var doc = XDocument.Load(path);
var items = doc.Descendants("HandlingData").Elements("Item");//.ToArray();
var query = from i in items
            select new 
            {
                HandlingName = (string)i.Element("handlingName"),
                Mass = (decimal?)i.Element("fMass").Attribute("value"), 
                InitialDragCoeff = (decimal?)i.Element("fInitialDragCoeff").Attribute("value"), 
                PercentSubmerged = (decimal?)i.Element("fPercentSubmerged").Attribute("value"), 
                DriveBiasFront = (decimal?)i.Element("fDriveBiasFront").Attribute("value"), 
                InitialDriveGears = i.Element("nInitialDriveGears").Attribute("value")
            }
string test = ("{0} - {1}" + query.First().HandlingName + query.First().Mass + query.First().InitialDragCoeff);
richTextBox1.Text = test;

Here's the XML Document :

<?xml version="1.0" encoding="UTF-8"?>

<CHandlingDataMgr>
  <HandlingData>
    <Item type="CHandlingData">
      <handlingName>Car1</handlingName>
      <fMass value="140000.000000" />
      <fInitialDragCoeff value="30.000000" />
      <fPercentSubmerged value="85.000000" />
      <vecCentreOfMassOffset x="0.000000" y="0.000000" z="0.000000" />
      <vecInertiaMultiplier x="1.000000" y="1.000000" z="1.000000" />
      <fDriveBiasFront value="1.000000" />
      <nInitialDriveGears value="1" />
    </Item>
    <Item type="CHandlingData">
      <handlingName>Car2</handlingName>
      <fMass value="180000.000000" />
      <fInitialDragCoeff value="7.800000" />
      <fPercentSubmerged value="85.000000" />
      <vecCentreOfMassOffset x="0.000000" y="0.000000" z="0.000000" />
      <vecInertiaMultiplier x="1.000000" y="1.300000" z="1.500000" />
      <fDriveBiasFront value="0.200000" />
      <nInitialDriveGears value="6" />
    </Item>
 </HandlingData>
</CHandlingDataMgr>   

As shown, there's multiple handling Name's. The CSharp code above does work, but only for the first block & I'm wondering how to make it read the same values from the different handling name.

I have tried :

if (query.First().HandlingName == "Car2")
{
    MessageBox.Show("Car 2 found");
}

but since the message box never appeared, I assume this code doesn't read the hole file?

I'm hoping for output like this:

Name: Car 1 
Mass: 140000.000000
InitialDragCoeff: 30.000000

Name: Car 2
Mass: 180000.000000
InitialDragCoeff: 7.800000

My problem in a 'nut shell' : Program does not see Car 2

Any help would be really appreciated, as I've tried many solutions & read many pages regarding XML today

Upvotes: 2

Views: 135

Answers (1)

Richard
Richard

Reputation: 109100

You have:

string test = ("{0} - {1}" + query.First().HandlingName + query.First().Mass 
         +  query.First().InitialDragCoeff);

that's only ever going to get you the first element, because that's what you asked for.

I think you probably want to loop:

foreach (var item in query) {
  var s = "{0} - {1}" + item.HandlingName + query.item.Mass 
         +  item.InitialDragCoeff
  // …
}

Upvotes: 1

Related Questions