I am new to XML. I need to parse this XML and read the values from the -Field- element and -name- attribute.
I need values from accountID, deviceID, odometerKM
Here is the XML:
<GTSResponse command="dbget" result="success">
<Record table="EventDataView" partial="true">
<Field name="accountID" primaryKey="true" alternateKeys="adtkey,driverkey">
<![CDATA[salesdemo]]>
</Field>
<Field name="deviceID" primaryKey="true" alternateKeys="adtkey">
<![CDATA[bubba_polaris]]>
</Field>
<Field name="timestamp" primaryKey="true" alternateKeys="adtkey,driverkey">1605919705</Field>
<Field name="statusCode" primaryKey="true">0xF010</Field>
<Field name="latitude">0.0</Field>
<Field name="longitude">0.0</Field>
<Field name="odometerKM">0.2566422</Field>
<Field name="odometerOffsetKM">0.0</Field>
</Record>
<Record table="EventDataView" partial="true">
<Field name="accountID" primaryKey="true" alternateKeys="adtkey,driverkey">
<![CDATA[salesdemo]]>
</Field>
<Field name="deviceID" primaryKey="true" alternateKeys="adtkey">
<![CDATA[bubba_polaris]]>
</Field>
<Field name="timestamp" primaryKey="true" alternateKeys="adtkey,driverkey">1605919705</Field>
<Field name="statusCode" primaryKey="true">0xF010</Field>
<Field name="latitude">0.0</Field>
<Field name="longitude">0.0</Field>
<Field name="odometerKM">0.23445323</Field>
<Field name="odometerOffsetKM">0.0</Field>
</Record>
</GTSResponse>
Here is the code I have tried:
XDocument doc = XDocument.Parse(receivedResponse);
Dictionary<string, string> dataDictionary = new Dictionary<string, string>();
foreach (XElement element in doc.Descendants().Where(p => p.HasElements == false))
{
int keyInt = 0;
string keyName = element.Name.LocalName;
while (dataDictionary.ContainsKey(keyName))
{
keyName = element.Name.Namespace.ToString();
keyName = element.Name.LocalName + "_" + keyInt++;
}
dataDictionary.Add(keyName, element.Value);
}
foreach (var x in dataDictionary)
{
Console.WriteLine("keyName: " + x.Key + " value: " + x.Value);
}
When I run this, it loops through all of the -Field- elements but it does not use the -name-. I need to see the -name- so I know what value I have. I will be updating my database and need to loop through and update fields accordingly by name.

