1

私は次のxmlを持っています:

<Places Count='50'>
<Place ID='1' Row='1' Place='1' Status='10' Fragment='0'></Place>
<Place ID='2' Row='1' Place='2' Status='10' Fragment='0'></Place>
<Place ID='3' Row='1' Place='3' Status='10' Fragment='0'></Place>
<Place ID='4' Row='1' Place='4' Status='10' Fragment='0'></Place>
//other tags
</Places>

これは私の方法です:

public static List<string> ParseFreePlace(string response, string level)
{
   if (string.IsNullOrWhiteSpace(response)) return new List<string>();

   return XDocument.Parse(response).Descendants("Place")
         .Select(element => (string) element.Attribute("Row")).ToList();
}

List<string>次のコンテンツを取得したい:

r=1;p=1;f=0;l=54; // 0 element in the List
r=1;p=2;f=0;l=54; // 1 element in the List
r=1;p=3;f=0;l=54; // 2 element in the List
r=1;p=4;f=0;l=54; // 3 element in the List

l内容は可変levelです。

4

1 に答える 1

2

代わりにこれを試してください:

public static List<string> ParseFreePlace(string response, string level)
{
   if (string.IsNullOrWhiteSpace(response)) return new List<string>();

   return 
        XDocument.Parse(response)
                 .Descendants("Place")
                 .Select(e =>
                    String.Format("r={0};p={1};f={2};l={3};",
                                   e.Attribute("Row").Value,
                                   e.Attribute("Place").Value,
                                   e.Attribute("Fragment").Value,
                                   level)
                 ).ToList();
}

このように呼び出された場合 (inputが XML 文字列であると仮定):

var lines = ParseFreePlace(input, "54");
foreach (var line in lines) Console.WriteLine(line);

それは私のためにこれを返します:

r=1;p=1;f=0;l=54;
r=1;p=2;f=0;l=54;
r=1;p=3;f=0;l=54;
r=1;p=4;f=0;l=54;
于 2012-05-14T12:16:26.387 に答える