I have a string formatted this way:

<?TAG param1="val1" parm2="val2" paramN="valN"  /><?TAG param1="val1" parm2="val2" paramN="valN"/><?TAG param1="val1" parm2="val2" paramN="valN"/>

"TAG" is always the same value, but number of occurrences is variable and the number of parameters for each occurrence too. I can't change the source format.

I need to get the list of parameters for each occurrence using C# (.NET 4.0) Can you help me out?

有帮助吗?

解决方案

XElement rootElement = XElement.Parse(string.Format("<element>{0}</element>", 
                                             yourString.Replace("?TAG", "TAG")));
var elements = rootElement.Elements();
var yourResult = elements.Select(x => new TagsAndParams { Tag = x,
    Params = x.Attributes.Where(xa => xa.Name.LocalName.BeginsWith("param") });

With this class as a result holder (I know I could use anonymous types, but this is better for passing to other functions):

public class TagsAndParams
{
    XElement Tag;
    IEnumerable<XAttribute> Params;
}

其他提示

You could do it with a nasty looking RegEx, but I'd make sure it's not actually an XML PI chain first:

(?<tag><?TAG (?<parm>param\d{1,2}=\"[^\"]+\"\s*)*\/\>)*

This will match groups, each group containing:

  • full tag
  • paramX="valX" pair
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class ExampleClass
{
    static void Main(string[] args)
    {
        string example = "<?TAG param1=\"val1\" param2=\"val2\" paramN=\"valN\" /><?TAG param1=\"val1\" param2=\"val2\" paramN=\"valN\"/><?TAG param1=\"val1\" param2=\"val2\" paramN=\"valN\"/>";
    List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
        string[] tokens = Regex.Split(example, "/><\\?TAG|<\\?TAG|/>");
        foreach (string token in tokens) if (token.Length > 0)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            string[] parms = token.Split(' ');
            foreach (string parm in parms) if (parm.Length > 0)
            {
                string[] keyvalue = Regex.Split(parm, "=\"|\"");
                parameters.Add(keyvalue[0], keyvalue[1]);
            }
            result.Add(parameters);
        }

    Console.WriteLine("TAGs detected: " + result.Count);
    foreach (Dictionary<string, string> token in result)
        {
            Console.WriteLine("TAG");
            foreach (KeyValuePair<string, string> kvp in token)
                Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        }
    }
}

I've finally solved using this code (provided by a friend of mine). The trick was the Regex used for splitting individual elements. Thank you for the support, I will make use of the tip about xml parser in future :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top