2016-04-18 4 views
2

ServerGroupによる作業項目の追跡が必要です。各ServerGroupは複数のFaultCodeを持つことがあります。LINQを使用した検索、追加、更新

私が使用するのに最適なXMLレイアウトは何ですか? オプションAまたはB?オプションAで検索するためにlinqクエリを作成できましたが、フォールトコードをチェックする方法がわかりませんでした。だから、私はServerGroupとFaultCodeをグループ化することができるので、オプションBを考えました。 xmlがオプションBのように構造化されている場合、linq検索を実行することは可能ですか?

ありがとうございました。

<?xml version="1.0" encoding="utf-16"?> 
<CapacityIssues xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    **//Option A** 
    <TrackingDetails> 
     <ServerGroup>A</ServerGroup> 
     <FaultCode>123</FaultCode> 
     <ID>4123567</ID> 
     <Title>Capacity Issues</Title> 
     <AssignedTo>Team A</AssignedTo> 
     <Description>Server Group A has an issue</Description> 
     <State>Active</State> 
     <Priority>2</Priority> 
    </TrackingDetails> 

    **//Option B** 
    <TrackingDetails ServerGroup="A" FaultCode="123"> 
     <ID>4123567</ID> 
     <Title>Capacity Issues</Title> 
     <AssignedTo>TeamA</AssignedTo> 
     <Description>This is a test</Description> 
     <State>Active</State> 
     <Priority>0</Priority> 
    </TrackingDetails> 

</CapacityIssues> 

私はオプションAを使用して検索することができた、ここで私が書いたコードです。

パブリック静的辞書> TFS(ストリングファイルパス) {//は XDocumentドキュメント= XDocument.Load(除いたファイルパスを指定して)読み取る原稿の種類を設定します。ここで

//execute reading of xml document 
    if (doc == null || doc.Root == null) 
    { 
     throw new ArgumentException($"Null document loaded from {filePath}"); 
    } 

    return doc.Root.Elements("TfsDetails").ToDictionary(r => 
     r.Element("ServerGroup").Value, 
     r => Tuple.Create(
      r.Element("FaultCode").Value, 
      r.Element("ID").Value, 
      r.Element("Title").Value, 
      r.Element("AssignedTo").Value, 
      r.Element("Description").Value, 
      r.Element("State").Value, 
      r.Element("Priority").Value 
      )); 
} 

は、私はオプションBの要素を追加する方法であるXMLファイルへ

private static void Create(string filePath) 
{ 
    //load xml file 
    XDocument doc = XDocument.Load(filePath); 

    //xmnl header 
    XElement root = new XElement("CapacityIssues"); 

    //node root 
    root.Add(new XAttribute("ServerGroup", "A")); 

    //cluster and TFS details 
    root.Add(new XAttribute("FaultCode", "111")); 
    root.Add(new XElement("ID", "4123567")); 
    root.Add(new XElement("Title", "Capacity Issues")); 
    root.Add(new XElement("AssignedTo", "Team A")); 
    root.Add(new XElement("Description", "ServerGroup A has an issue")); 
    root.Add(new XElement("State", "Active")); 
    root.Add(new XElement("Priority", "0")); 

    //add new element 
    doc.Element("CapacityIssues").Add(root); 

    //save xml file 
    doc.Save(filePath); 
} 
+0

オプションbはxml部分のAより冗長ではありません。確かに、XDoc n XElementを使って、それをモデル化するのは簡単でしょう。 –

+0

これは 'XElement root = new XElement(" TrackingDetails ")とされています。 '? –

答えて

1

両方はもちろん、有効であるが、それはあまりを持っているよう勧告は、オプションB(使用はむしろ要素よりも属性)ですが深さ

linqをAttributesとして使用することができます。

foreach (var xAttribute in root.Descendants("TrackingDetails").Attributes().Where(o => o.Name.Equals("ServerGroup"))) 
{ 
    // do something about ServerGroup A or B 
} 
関連する問題