2017-12-10 18 views
0
<SubTexture name="explosion0000.png" x="180" y="474" width="24" height="25"/> 
<SubTexture name="explosion0001.png" x="422" y="609" width="30" height="30"/> 
<SubTexture name="explosion0002.png" x="395" y="981" width="34" height="34"/> 
<SubTexture name="explosion0003.png" x="354" y="981" width="39" height="39"/> 

以上は、情報をリストから抽出する必要がある4行のXMLファイルです。私はそれぞれの行を読んで、文字列と値を使って特定のタスクを行う必要があります。これを行うために、私はコードxmlファイルを1行ずつ読み込んでリストに追加する

{ 
     XmlReader xmlReader = XmlReader.Create("D:/C#/GameAssets/Images/alienExplode/images/alienExplode.xml"); 
     while (xmlReader.Read()) 
     { 
      Console.Write(xmlReader.Name); 
      while (xmlReader.MoveToNextAttribute()) // Read the attributes. 
       Console.Write(" " + xmlReader.Name + " = '" + xmlReader.Value + "' "); 
      Console.WriteLine(" "); 
     } 
     Console.ReadKey(); // wait for keyboard input to exit 
    }` 

を使用するが、私は、それを読んでデータを抽出し、それを使用できるように、リストに値を取得する方法がわかりません。助けてもらえますか?

+3

* "xmlファイルを1行ずつ読み込む" * XMLの "行"の概念は存在しません。これらの点でそれについて考えることはあなたの頭の中から取り除くことは役に立たない。あなたがしたいのは、* XMLを解析し、それを要素ごとに処理することです。 – Tomalak

+0

上記のXMLを使用してサンプルコードのいくつかの行でそれを行うのを手伝ってもらえますか? – emorphus

+0

'XmlReader'を使う特別な理由はありますか、それともあなたが試した最初のものですか?また、あなたが何か起こっているか、生産したいことを正確に文章で説明してください。 – Tomalak

答えて

0

私は以下のコードで上記の問題を解決しました。 xmlファイルはhereです。

using System; 
using System.Collections.Generic; 
using System.Xml; 

namespace ReadXMLfromFile 
{ 
    /// <summary> 
    /// Summary description for Class1. 
    /// </summary> 
    class Class1 
    { 
     static bool addToFile = false; 
     static List<List<string>> imageData = new List<List<string>>(); 

     static void Main(string[] args) 
     { 

      XmlReader xmlReader = XmlReader.Create("D:/C#/GameAssets/Images/alienExplode/images/alienExplode.xml"); 
      while (xmlReader.Read()) 
      { 
       List<string> tempData = new List<string>(); 

       while (xmlReader.MoveToNextAttribute()) 
       { 
        Console.Write(xmlReader.Value + " "); 
        tempData.Add(xmlReader.Value); 
        Console.WriteLine("-----------------------------> adding data to tempList " + tempData.Count); 
        //Console.ReadKey(); 
        addToFile = true; // get ready to write 


       } 
       if (addToFile) 
       { 
        Console.WriteLine("adding tempList to Mainlist"); 
        imageData.Add(tempData); 

        Console.WriteLine("----------------------------> imagedata " + imageData.Count); 

        addToFile = false; 

       } 

      } 
      Console.WriteLine(imageData.Count); 

      for (int i = 1; i <= imageData.Count - 1; i++) 
      { 
       for (int x = 0; x < imageData[i].Count; x++) 
       { 
        Console.WriteLine(imageData[i][x]); 
       } 
      } 
      Console.ReadKey(); // wait for keyboard input to exit 
     } 
    } 
} 

この例で示すように、xmlファイルを「1行ずつ」読むことができました。この特定のファイルは、最初のノードを除くすべてのノードが同じ形式であるためです。

最初のforループのカウンタは、イメージリストの名前である最初のエントリを避けているので、1から始まります。

何か問題があった場合は、私に修正してください。私はまだ学んでいます。

1

クイックソリューション(しかし、ハックと...)

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

namespace ConsoleApp 
{ 
    class Program 
    { 
     static void Main (string[] args) 
     { 
      var xml = @"<SubTexture name=""explosion0000.png"" x=""180"" y=""474"" width=""24"" height=""25""/> 
         <SubTexture name=""explosion0001.png"" x=""422"" y=""609"" width=""30"" height=""30""/> 
         <SubTexture name=""explosion0002.png"" x=""395"" y=""981"" width=""34"" height=""34""/> 
         <SubTexture name=""explosion0003.png"" x=""354"" y=""981"" width=""39"" height=""39""/>"; 

      var xElement = XElement.Parse ("<rootHack>" + xml + "</rootHack>"); // XElement requires root element, so we need to use a hack 

      var list = xElement.Elements().Select (element => new MyClass 
      { 
       Name = element.Attribute ("name").Value, 
       X  = int.Parse (element.Attribute ("x").Value), 
       Y  = int.Parse (element.Attribute ("y").Value), 
       Width = int.Parse (element.Attribute ("width").Value), 
       Height = int.Parse (element.Attribute ("height").Value), 

      }).ToList(); 
     } 
    } 

    class MyClass 
    { 
     public string Name; 
     public int X, Y, Width, Height; 

     public override string ToString() => $"{Name}, X={X} Y={Y}, W={Width} H={Height}"; 
    } 
} 
関連する問題