0

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?

4

3 回答 3

3
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") });

使用这个类作为结果持有者(我知道我可以使用匿名类型,但这更适合传递给其他函数):

public class TagsAndParams
{
    XElement Tag;
    IEnumerable<XAttribute> Params;
}
于 2013-07-18T21:12:25.757 回答
0
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);
        }
    }
}

我终于用这段代码解决了(由我的朋友提供)。诀窍是用于拆分单个元素的正则表达式。感谢大家的支持,我以后会用到xml解析器的小技巧:)

于 2013-07-20T07:20:28.267 回答
0

你可以用一个讨厌的外观来做到这一点RegEx,但我首先要确保它实际上不是一个 XML PI 链:

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

这将匹配组,每个组包含:

  • 完整标签
  • paramX="valX" 对
于 2013-07-18T21:11:01.933 回答