2011-10-03 60 views
0

有趣的是,隨着下面的類我知道我的輸出應該如何,但我無法弄清楚如何爲它保存數據。讓一個班級跟蹤相關的字符串

請參閱下面的代碼:

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

所以我想做的事,就是我希望能夠somohe涉及三個不同的字符串「設備,組針」 somhow一個PIN屬於GROUP和一個GROUP屬於DEVICE。這怎麼可能?

如果我不夠清楚,請讓我知道。


UPDATE 好吧,我有,我可以沒有問題從中讀取數據的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(可有很多設備的)收集數據,並使用我上面寫的類,過濾器失敗的引腳。

+1

不,這還不夠清楚。更清楚地描述數據結構。 –

+0

@大衛我更新了更多的信息的問題。我不確定現在是否清楚。讓我知道了! –

+0

小問題,是不是標籤嵌套在一些標籤? –

回答

1

我已經寫了一些代碼,你可以用它來讀取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, 
1

我會實現對象數據模型,用三類:

DEVICE HAS GROUP

GROUP HAS PIN


UPDATE

第1類:DEVICE,與成員字段list_of_groups(可以使用不同的名稱)

2類:GROUP,與構件字段list_of_pins

3類:PIN,與構件字段result(布爾值)

+0

您能否展示這種方式的快速實施示例? –

2

類似的東西?

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

那麼我如何才能保持這三類之間的關係,而我可能有多個設備? –

1

我認爲這是更好,如果你用這種方式改變的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塔包含Group列表包含對象Pin的列表。