创建一个类来跟踪相关的字符串

本文关键字:跟踪 字符串 一个 创建 | 更新日期: 2023-09-27 18:10:29

这是一种有趣的,使用下面的类,我知道我的输出应该是什么样的,但我不能弄清楚如何为它保存数据。

请参阅下面的代码:

    public class QuickFailureReportText
{
    public string[] device { get; set; }
    public 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();
    }
}

我想做的是,我想把三个不同的字符串"device, group, pin"联系起来一个pin属于一个group,一个group属于一个device。这怎么可能呢?

如果我说的不够清楚,请告诉我。


好的,我有一个XML文件,我可以从它读取数据,没有问题。XML文件看起来像这样:

<?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>

所以我想从这个XML(可能有很多设备)中收集数据,并使用我上面写的类过滤失败的引脚。

创建一个类来跟踪相关的字符串

是这样吗?

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;
}

我已经写了一些代码,您可以使用从xml文件中读取所需的信息,存储在设备变量

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();
    }
}

下面是示例文件的's'字符串的值:

Quick Failure Report
Failures in device 1
Group group 1 : A1, 
Group group 2 : B1, 

我将使用三个类实现一个对象数据模型:

DEVICE HAS GROUP

GROUP HAS PIN


类1:DEVICE,成员字段list_of_groups(您可以使用不同的名称)

类2:GROUP,成员字段list_of_pins

Class 3: PIN,成员字段result (boolean)

我认为这样更改xml会更好:

<?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> 

所以你可以定义对象Device,它包含一个包含对象Pin的列表的Group的列表。