My XML file looks like:
<device name="dev. 1" nodes="16" scenarios="20">
<package>Pack. 1</package>
<info>info...</info>
<picture>pic</picture>
<nodes>
<node no="1" name="OUT_1" type="source"/>
<node no="2" name="OUT_2" type="source"/>
<node no="3" name="OUT_3" type="source"/>
<node no="4" name="OUT_4" type="source"/>
</nodes>
<scenario name="scenario 1">
<zth m_node="1" d_node="1" model="table" points="190">
<data time="1" value="0.1"/>
<data time="2" value="2"/>
<data time="2" value="4"/>
</zth>
<zth m_node="1" d_node="2" model="table" points="190">
<data time="1" value="0.3"/>
<data time="2" value="4"/>
</zth>
</scenario>
<scenario name="scenario 2">
<zth m_node="1" d_node="1" model="table" points="190">
<data time="2" value="2"/>
<data time="1" value="0.3"/>
<data time="2" value="4"/>
</zth>
<zth m_node="1" d_node="2" model="table" points="190">
<data time="1" value="0.3"/>
<data time="2" value="4"/>
</zth>
</scenario>
</device>
The data I need to read is:
The Attribute name from the Element scen from the is in the <data>, I need to get the time and value from the data into arrays but only when the d_node="2" in the <zth>.
This is what I have tried:
public class Scenario {
public string name { get; set; }
public List<string> ZthList { get; set; }
}
public class Zth {
public string m_node { get; set; }
public string d_node { get; set; }
public string time { get; set; }
public string value { get; set; }
}
public class XMLReader
{
public void xmlReader()
{
string currentDir = Environment.CurrentDirectory;
//getting the directory
DirectoryInfo directory = new DirectoryInfo(
Path.GetFullPath(Path.Combine(currentDir, @"..\..\" + @"utils\XML\XMLFile1.xml")));
XDocument doc = XDocument.Load(Path.Combine(currentDir, @"..\..\" + @"utils\XML\XMLFile1.xml"));
var scenarios = (from s in doc.Root.Elements("scenario")
select new Scenario{
name = (string)s.Attribute("name"),
ZthList = s.Elements("zth")
.Select(r => new Zth
{
m_node = (string)r.Attribute("m_node"),
d_node = (string)r.Attribute("d_node"),
time = (string)r.Element("data").Attribute("time"),
value = (string)r.Element("data").Attribute("value"),
}).ToList()
}).ToList();
var zth_d_node = scenarios.Where(x => x.ZthList.Any(r => r.d_node == "1")).ToList();
var s_names = scenarios.Where(x => x.Element("").Value.Equals("name")).toList();
Console.WriteLine("List: ");
Console.WriteLine(String.Join(", ", scenarios));
}
}
I am getting and error: Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<project1.utils.Zth>' to 'System.Collections.Generic.List<string>'
Also the data from the time and value is of type double I changed it to string because I was getting a similar error but still it's not working

