2017-06-08 5 views
0

ユーザーが提供するソート属性値と子値によってXMLELEMENTをソート

<root> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1496761802">06/06/2017 03:10:02 PM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> abc</Cell> 
 
\t \t <Cell columnid="4"> None</Cell> 
 
\t \t <Cell columnid="5"> 1 </Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7"> efg</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1496743990">06/06/2017 10:13:10 AM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell>3 
 
\t \t <Cell columnid="3"> o</Cell> 
 
\t \t <Cell columnid="4"> None</Cell> 
 
\t \t <Cell columnid="5"> 1 </Cell> 
 
\t \t <Cell columnid="6"> N/A </Cell> 
 
\t \t <Cell columnid="7"> ice age</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1496227357">31/05/2017 10:42:37 AM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> Microsoft-Windows-CAPI2</Cell> 
 
\t \t <Cell columnid="4"> N/A</Cell> 
 
\t \t <Cell columnid="5"> 513</Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7">Access is denied.. </Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1495568786">23/05/2017 07:46:26 PM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> Microsoft-Windows-Immersive-Shell</Cell> 
 
\t \t <Cell columnid="4"> N/A</Cell> 
 
\t \t <Cell columnid="5">2484</Cell> 
 
\t \t <Cell columnid="6">SR</Cell> 
 
\t \t <Cell columnid="7">hello</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1495568789">23/05/2017 07:46:29 PM</Cell> 
 
\t \t <Cell columnid="2">Error</Cell> 
 
\t \t <Cell columnid="3"> Application Hang </Cell> 
 
\t \t <Cell columnid="4"> N/A</Cell> 
 
\t \t <Cell columnid="5">1002</Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7"> here is a error</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1495568740">23/05/2017 07:45:40 PM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> Application Error</Cell> 
 
\t \t <Cell columnid="4"> Application Crashing Events</Cell> 
 
\t \t <Cell columnid="5">1000</Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7"> error number 3</Cell> 
 
\t </SectionItem> 
 
</root>

私はセル属性のセルの要素の値にsectionItem要素ベースをソートしたい場合は、ユーザーがどのようにすることができます与えColumnIDの私はこれに近づく?前もって感謝します。

ユーザーがcolunidに "1"を指定した場合、columnid = "1"のcellの値でsectionItemの基底をソートする必要があります。 ユーザがcolunidに "3"を指定した場合、sectionidemのbaseをcolumnid = "3"のCellの値にソートする必要があります。

私は質問をま​​とめるのが難しいため、質問を理解しやすくしたいと考えています。

私は以下の出力を取得する必要カラムID = "3"

でソートしていた場合:あなたは本当にデータテーブルを持っているとmehtodのDataTableを使用する必要があります

<root> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1496761802">06/06/2017 03:10:02 PM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> abc</Cell> 
 
\t \t <Cell columnid="4"> None</Cell> 
 
\t \t <Cell columnid="5"> 1 </Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7"> efg</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1495568740">23/05/2017 07:45:40 PM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> Application Error</Cell> 
 
\t \t <Cell columnid="4"> Application Crashing Events</Cell> 
 
\t \t <Cell columnid="5">1000</Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7"> error number 3</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1495568789">23/05/2017 07:46:29 PM</Cell> 
 
\t \t <Cell columnid="2">Error</Cell> 
 
\t \t <Cell columnid="3"> Application Hang </Cell> 
 
\t \t <Cell columnid="4"> N/A</Cell> 
 
\t \t <Cell columnid="5">1002</Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7"> here is a error</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1496227357">31/05/2017 10:42:37 AM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> Microsoft-Windows-CAPI2</Cell> 
 
\t \t <Cell columnid="4"> N/A</Cell> 
 
\t \t <Cell columnid="5"> 513</Cell> 
 
\t \t <Cell columnid="6"> N/A</Cell> 
 
\t \t <Cell columnid="7">Access is denied.. </Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1495568786">23/05/2017 07:46:26 PM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell> 
 
\t \t <Cell columnid="3"> Microsoft-Windows-Immersive-Shell</Cell> 
 
\t \t <Cell columnid="4"> N/A</Cell> 
 
\t \t <Cell columnid="5">2484</Cell> 
 
\t \t <Cell columnid="6">SR</Cell> 
 
\t \t <Cell columnid="7">hello</Cell> 
 
\t </SectionItem> 
 
\t <SectionItem> 
 
\t \t <Cell columnid="1" InSeconds="1496743990">06/06/2017 10:13:10 AM</Cell> 
 
\t \t <Cell columnid="2"> Error</Cell>3 
 
\t \t <Cell columnid="3"> o</Cell> 
 
\t \t <Cell columnid="4"> None</Cell> 
 
\t \t <Cell columnid="5"> 1 </Cell> 
 
\t \t <Cell columnid="6"> N/A </Cell> 
 
\t \t <Cell columnid="7"> ice age</Cell> 
 
\t </SectionItem> 
 
</root>

+0

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overviewにお読みください。 linqを使ってすべてのSectionItem要素を取得した後、columnid属性に基づいて(下位の)Cell要素の値を選択し、結果に対してSort()を呼び出すように思えます。あなたがこのようなことから期待していることは本当に明確ではありません。 SectionItem属性?、Cell値?とにかく、それはかなりまっすぐ前方linqクエリで行うことができる何かのように見えます。 – Dweeberly

+0

混乱して申し訳ありません。 sectionItem要素をソート順に取得しようとしています。 – Terry

答えて

0

を。 ReadXml()を使用していますが、すべての実際のxmlデータを投稿していないため、入力したXMLを解析できませんでした。代わりに、私はカスタムxml linqパーサーを使用して、xml入力にデータテーブルを解析しました。それはまた、あなたがinnertexに余分なスペースを追加するのを助けなかったので、スペースを整えなければなりませんでした。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 
using System.Data; 
using System.Globalization; 


namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("Time", typeof(DateTime)); 
      dt.Columns.Add("Results", typeof(string)); 
      dt.Columns.Add("Name", typeof(string)); 
      dt.Columns.Add("Col 4", typeof(string)); 
      dt.Columns.Add("ID", typeof(int)); 
      dt.Columns.Add("Col 5", typeof(string)); 
      dt.Columns.Add("Status", typeof(string)); 


     //<Cell columnid="1" InSeconds="1496227357">31/05/2017 10:42:37 AM</Cell> 
     //<Cell columnid="2"> Error</Cell> 
     //<Cell columnid="3"> Microsoft-Windows-CAPI2</Cell> 
     //<Cell columnid="4"> N/A</Cell> 
     //<Cell columnid="5"> 513</Cell> 
     //<Cell columnid="6"> N/A</Cell> 
     //<Cell columnid="7">Access is denied.. </Cell> 

      XDocument doc = XDocument.Load(FILENAME); 

      foreach (XElement sectionItem in doc.Descendants("SectionItem")) 
      { 
       XElement[] cells = sectionItem.Elements("Cell").ToArray(); 
       DataRow newRow = dt.Rows.Add(new object[] { 
        DateTime.ParseExact((string)cells[0],"dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture), 
        ((string)cells[1]).Trim(), 
        ((string)cells[2]).Trim(), 
        ((string)cells[3]).Trim(), 
        (int)cells[4], 
        ((string)cells[5]).Trim(), 
        ((string)cells[6]).Trim(), 

       }); 
      } 

      dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("Name")).CopyToDataTable(); 

     } 

    } 

} 
0

以下は、私がコメントをしたときの私の考えの例です。

var cells = xdoc.Element("root") 
       .Elements("SectionItem") 
       .Elements("Cell") 
       .Where(x => x.Attribute("columnid").Value == sortCol) 
       .OrderBy(x => ParseCol(sortCol, x)); 

これは、要素「ルート」から始まり、SORTCOLに一致するものを選択し、「セル」すべてのサブ要素の上に「SectionItem」すべてのそれのサブ要素の上に列挙し、このの心は、次のLINQ文はあることに注意してください(XML文字列データの適切な型変換を提供するParseCol関数から返される型に基づいて列挙型をソートします)

私はjdwengのDataTableのアプローチが好きです。特にこれを何らかの形で表示する場合鳥のリスト/リスト。

using System; 
using System.Linq; 
using System.Xml.Linq; 

namespace ParseXml { 
class Program { 
    static void Main(string[] args) { 

     var xml = @" 
<root> 
<SectionItem> 
    <Cell columnid='1' InSeconds='1496761802'>06/06/2017 03:10:02 PM</Cell> 
    <Cell columnid='2'> Error</Cell> 
    <Cell columnid='3'> abc</Cell> 
    <Cell columnid='4'> None</Cell> 
    <Cell columnid='5'> 1 </Cell> 
    <Cell columnid='6'> N/A</Cell> 
    <Cell columnid='7'> efg</Cell> 
</SectionItem> 
<SectionItem> 
    <Cell columnid='1' InSeconds='1496743990'>06/06/2017 10:13:10 AM</Cell> 
    <Cell columnid='2'> Error</Cell>3 
    <Cell columnid='3'> o</Cell> 
    <Cell columnid='4'> None</Cell> 
    <Cell columnid='5'> 1 </Cell> 
    <Cell columnid='6'> N/A </Cell> 
    <Cell columnid='7'> ice age</Cell> 
</SectionItem> 
<SectionItem> 
    <Cell columnid='1' InSeconds='1496227357'>31/05/2017 10:42:37 AM</Cell> 
    <Cell columnid='2'> Error</Cell> 
    <Cell columnid='3'> Microsoft-Windows-CAPI2</Cell> 
    <Cell columnid='4'> N/A</Cell> 
    <Cell columnid='5'> 513</Cell> 
    <Cell columnid='6'> N/A</Cell> 
    <Cell columnid='7'>Access is denied.. </Cell> 
</SectionItem> 
<SectionItem> 
    <Cell columnid='1' InSeconds='1495568786'>23/05/2017 07:46:26 PM</Cell> 
    <Cell columnid='2'> Error</Cell> 
    <Cell columnid='3'> Microsoft-Windows-Immersive-Shell</Cell> 
    <Cell columnid='4'> N/A</Cell> 
    <Cell columnid='5'>2484</Cell> 
    <Cell columnid='6'>SR</Cell> 
    <Cell columnid='7'>hello</Cell> 
</SectionItem> 
<SectionItem> 
    <Cell columnid='1' InSeconds='1495568789'>23/05/2017 07:46:29 PM</Cell> 
    <Cell columnid='2'>Error</Cell> 
    <Cell columnid='3'> Application Hang </Cell> 
    <Cell columnid='4'> N/A</Cell> 
    <Cell columnid='5'>1002</Cell> 
    <Cell columnid='6'> N/A</Cell> 
    <Cell columnid='7'> here is a error</Cell> 
</SectionItem> 
<SectionItem> 
    <Cell columnid='1' InSeconds='1495568740'>23/05/2017 07:45:40 PM</Cell> 
    <Cell columnid='2'> Error</Cell> 
    <Cell columnid='3'> Application Error</Cell> 
    <Cell columnid='4'> Application Crashing Events</Cell> 
    <Cell columnid='5'>1000</Cell> 
    <Cell columnid='6'> N/A</Cell> 
    <Cell columnid='7'> error number 3</Cell> 
</SectionItem> 
</root>"; 

    var sortCol = "3"; 
    var xdoc = XDocument.Parse(xml); 
    var cells = xdoc.Element("root") 
        .Elements("SectionItem") 
        .Elements("Cell") 
        .Where(x => x.Attribute("columnid").Value == sortCol) 
        .OrderBy(x => ParseCol(sortCol, x)); 

    var newDoc = new XDocument(); 
    var root = new XElement("root"); 
    newDoc.Add(root); 
    foreach (var c in cells) { 
     root.Add(c.Parent); 
     } 

    Console.WriteLine(newDoc.ToString(SaveOptions.None)); 
    } 

    static object ParseCol(string col, XElement x) { 
     switch (col) { 
     case "1": 
      return long.Parse(x.Attribute("InSeconds").Value); 
     case "2": 
     case "3": 
     case "4": 
      return x.Value.Trim(); 
     case "5": 
      return long.Parse(x.Value); 
     case "6": 
     case "7": 
      return x.Value.Trim(); 
     default: 
      throw new ArgumentException("Bad column id string"); 
     } 
    } 
} 

}

関連する問題