It is kind of funny, With the follwing class I know how my output should like but I could not figure it out how to hold the data for it.
Please see the code below:
public class QuickFailureReportText
{
public string[] device { get; set; }
public开发者_Go百科 string[] group { get; set; }
public string[] pin { get; set; }
public override string ToString()
{
TextWriter tw;
StringBuilder sb = new StringBuilder();
tw = new StringWriter(sb);
tw.WriteLine("Quick Failure Report");
foreach (string dev in device)
{
tw.WriteLine("Failures in " + dev);
foreach (string grp in group)
{
tw.Write("Group " + grp + " : ");
foreach(string p in pin)
{
tw.Write(p + ", ");
}
tw.WriteLine(); //new line
}
tw.WriteLine(); //new line
}
return tw.ToString();
}
}
So what I want to do, is I want be able to somohe relate the three different string "device, group, pin" somhow that a PIN belongs to a GROUP and a GROUP belongs to a DEVICE. how that can be possible?
Please let me know if I am not clear enough.
UPDATE Ok, I have a XML file that I can read data from it with no problem. the xml file looks like something like this:
<?xml version="1.0" encoding="utf-8"?>
<DEVICES>
<device>
<name>device 1</name>
<groups>
<group>
<group_name>group 1</group_name>
<pins>
<pin result="fail">A1</pin>
<pin result="pass">A2</pin>
</pins>
</group>
<group>
<group_name>group 2</group_name>
<pins>
<pin result="fail">B1</pin>
<pin result="pass">B2</pin>
</pins>
</group>
</groups>
</device>
</DEVICES>
So I want to gather the data from this XML(which may have a lot of devices) and using the class I wrote above, filter the failed pins.
Something like that?
public class Device
{
public string Name;
public List<Group> Groups = new List<Group>();
}
public class Group
{
public string Name;
public List<Pin> Pins = new List<Pin>();
}
public class Pin
{
public string Name;
public string Result;
}
I've written some code you can use to read required information from xml file, store in devices variable
public class Device
{
public string Name;
public Dictionary<string, Group> Groups = new Dictionary<string, Group>();
}
public class Group
{
public string Name;
public List<string> Pins = new List<string>();
}
public class QuickFailureReportText
{
public Dictionary<string, Device> devices = new Dictionary<string, Device>();
public void AddLog(string deviceName, string groupName, string pin)
{
if (!devices.ContainsKey(deviceName))
devices.Add(deviceName, new Device()
{ Name = deviceName, Groups = new Dictionary<string, Group>() });
if (!devices[deviceName].Groups.ContainsKey(groupName))
devices[deviceName].Groups.Add(groupName, new Group()
{ Name = groupName, Pins = new List<string>() });
devices[deviceName].Groups[groupName].Pins.Add(pin);
}
public override string ToString()
{
TextWriter tw;
StringBuilder sb = new StringBuilder();
tw = new StringWriter(sb);
tw.WriteLine("Quick Failure Report");
XDocument xDoc = XDocument.Load(@"devices.xml");
foreach (XElement device in xDoc.XPathSelectElements("DEVICES/device"))
{
foreach (XElement group in device.XPathSelectElements("groups/group"))
{
foreach (XElement pin in group.XPathSelectElements("pins/pin"))
{
if (pin.Attribute("result").Value == "fail")
{
AddLog(device.XPathSelectElement("name").Value,
group.XPathSelectElement("group_name").Value, pin.Value);
}
}
}
}
foreach (var device in devices.Values)
{
tw.WriteLine("Failures in " + device.Name);
foreach (var grp in device.Groups.Values)
{
tw.Write("Group " + grp.Name + " : ");
foreach (string p in grp.Pins)
{
tw.Write(p + ", ");
}
tw.WriteLine(); //new line
}
tw.WriteLine(); //new line
}
return tw.ToString();
}
}
class Program
{
static void Main(string[] args)
{
string s = new QuickFailureReportText().ToString();
}
}
Below is value of 's' string for your example file:
Quick Failure Report
Failures in device 1
Group group 1 : A1,
Group group 2 : B1,
I would implement an object data model, using three classes:
DEVICE
HAS GROUP
GROUP
HAS PIN
UPDATE
Class 1: DEVICE
, with member field list_of_groups
(you can use a different name)
Class 2: GROUP
, with member field list_of_pins
Class 3: PIN
, with member field result
(boolean)
I think it's better if you change the xml in this way:
<?xml version="1.0" encoding="utf-8"?>
<DEVICES>
<device>
<name>device 1</name>
<groups>
<group>
<group_name>group 1</group_name>
<pins>
<pin result="fail">A1</pin>
<pin result="pass">A2</pin>
</pins>
</group>
<group>
<group_name>group 2</group_name>
<pins>
<pin result="fail">B1</pin>
<pin result="pass">B2</pin>
</pins>
</group>
</groups>
</device>
</DEVICES>
So you can define the object Device
tha contains a List of Group
that contains a list of object Pin
.
精彩评论