Here is the code my goal is to iterate over all the nodes and not just one so in my case it gets the first Device Node and all his children at once so how can i get every single Device and PortA PortB nodes. then i will be able to set my class values.
private void loadXmlFile(string path)
{
try
{
XElement deviceElement = XElement.Load(path);
Device device = new Device();
var allElements = deviceElement.DescendantNodes();
foreach (var elm in deviceElement.Elements())
{
if (elm.Name == "Device")
{
foreach (XAttribute att in elm.Attributes())
{
if (att.Name == "Type")
{
device.TBType1 = att.Value.ToString();
}
if (att.Name == "Name")
{
device.Name = att.Value.ToString();
}
if (att.Name == "ParentConnectedToPort")
{
device.ParentConnectedTo = att.Value.ToString();
}
if (att.Name == "DeviceConnectedToPort")
{
device.DeviceConnectedTo = att.Value.ToString();
}
string connectedTo = (string)elm.Parent.Attribute("Connected_BY");
if (att.Name == "properties" && connectedTo == "Directly")
{
string str = att.Value;
string[] parts = str.Split(':', ',');
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Here is my XML file looks like need to get every device element and its own values and set out to my class values .
<CMS>
<Device Name="CM_HOST" Type="TR">
<PortA Connected_BY="Directly">
<Device TB="AR" ParentConnectedToPort="A" Name="Akitio" DeviceConnectedToPort="A" properties="{'Type': 'AR' ,'Name': 'Akitio','Cable': '20G Passive'}">
<PortB Connected_BY="Directly">
<Device TB="AR" ParentConnectedToPort="A" Name="doc1" DeviceConnectedToPort="B" properties="{'Type': 'AR' ,'Name': 'doc1','Cable': '20G Passive'}">
<PortB Connected_BY="Directly">
<Device TB="AR" ParentConnectedToPort="A" Name="doc2" DeviceConnectedToPort="B" properties="{'Type': 'AR' ,'Name': 'doc2','Cable': '20G Passive'}" />
</PortB>
<PortA Connected_BY="None" />
<PortE Connected_BY="None" />
</Device>
</PortB>
<PortA Connected_BY="None" />
<PortE Connected_BY="None" />
</Device>
</PortA>
<PortB Connected_BY="None" />
<PortE Connected_BY="None" />
</Device>
<Sx properties="{'FTDI_port': '0' ,'SX_power_button_pin': '7','SX_SLP_S3_pin': '6','SX_SLP_S4_pin': '11','SX_SLP_S5_pin': '10','SX_TBT_wake_N_pin': '8','SX_PCIE_wake_pin': '9','G3_Power_Mode': 'PowerSplitter'}" />
</CMS>