C#で以下のXMLファイルをXDocumentで読み込み、自作クラスに格納しようとしています。
xml
1<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="300px" height="300px" viewBox="0 0 300 300" preserveAspectRatio="xMidYMid meet"> 2<g id ="ue_chu_matome"> 3 <g id ="ue_kyokusen" fill="#3a3a3a" stroke="black"> 4 <path d="M10 10 C 20 20, 40 20, 50 10Z" stroke="black" fill="transparent"/> 5 <path d="M70 10 C 70 20, 120 20, 120 10Z" stroke="black" fill="transparent"/> 6 </g> 7 8 <g id ="chu_kyokusen" fill="#3a3a3a" stroke="black"> 9 <path d="M10 60 C 20 80, 40 80, 50 60Z" stroke="black" fill="transparent"/> 10 </g> 11</g> 12 13<g id ="shita_kyokusen" fill="#3a3a3a" stroke="black"> 14 <path d="M10 110 C 20 140, 40 140, 50 110Z" stroke="black" fill="transparent"/> 15</g> 16</svg>
上記のようにgタグでグルーピングされたpathタグが存在しており、
そのpathタグはどのグループに属しているかを判別しようとしています。
ただしgタグの子にさらにgタグが居たとしても、
一番親のgタグに属しているものとして判別したいです。
現在の実装は以下の通りです。
C#
1 public class SVGDataList 2 { 3 // プロパティ用変数の定義 4 private string _gid; 5 private List<String> _PathList; 6 7 public SVGDataList(XElement value) 8 { 9 // コンストラクタでプロパティに格納 10 var pathes = value.Descendants("{http://www.w3.org/2000/svg}path"); 11 foreach (XElement path in pathes) 12 { 13 _PathList.Add(path.Attribute("d").Value); 14 } 15 _gid = value.Attribute("id").Value; 16 } 17 18 // プロパティ 19 public string Gid 20 { 21 set 22 { 23 _gid = value; 24 } 25 get 26 { 27 return _gid; 28 } 29 } 30 31 public List<String> PathList 32 { 33 get 34 { 35 return _PathList; 36 } 37 } 38 } 39 40 class Program 41 { 42 static void Main(string[] args) 43 { 44 XDocument table = XDocument.Load(@ファイルのパス"); 45 var gid = table.Descendants("{http://www.w3.org/2000/svg}g"); 46 47 foreach (XElement g in gid) 48 { 49 SVGDataList svgdata = new SVGDataList(g); 50 Console.WriteLine(svgdata.Gid + ":"); 51 foreach (string str in svgdata.PathList) 52 Console.WriteLine("\t" + str); 53 } 54 Console.ReadKey(); 55 } 56 }
結果は以下のように出力されます。
ue_chu_matome:
M10 10 C 20 20, 40 20, 50 10Z
M70 10 C 70 20, 120 20, 120 10Z
M10 60 C 20 80, 40 80, 50 60Z
ue_kyokusen:
M10 10 C 20 20, 40 20, 50 10Z
M70 10 C 70 20, 120 20, 120 10Z
chu_kyokusen:
M10 60 C 20 80, 40 80, 50 60Z
shita_kyokusen:
M10 110 C 20 140, 40 140, 50 110Z
やりたいことは、ue_kyokusenとchu_kyokusenはmatomeで取得済なので、
matome、shita_kyokusenの2要素のみをSVGDataListクラスに格納したいです。
何か良いアイデアはございませんでしょうか。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/01 12:37