从此 XML 标记中提取选定节点值的方法

发布于 2024-07-27 01:47:54 字数 630 浏览 3 评论 0原文

鉴于下面列出的(样本 - 真实标记可能要复杂得多)标记和约束,任何人都可以提出一个比遍历整个树来检索 { "@@value1@@", "@@ 更有效/高效的解决方案 (C#) value2@@", "@@value3@@" },即实际使用标记时将被替换的标记列表。

注意:我无法控制标记、标记结构或要替换的标记的格式/命名。

<markup>
    <element1 attributea="blah">@@value1@@</element1>
    <element2>@@value2@@</element2>
    <element3>
        <element3point1>@@value1@@</element3point1>
        <element3point2>@@value3@@</element3point2>
        <element3point3>apple</element3point3>
    <element3>
    <element4>pear</element4>
</markup>

Given the (specimen - real markup may be considerably more complicated) markup and constraints listed below, could anyone propose a solution (C#) more effective/efficient than walking the whole tree to retrieve { "@@value1@@", "@@value2@@", "@@value3@@" }, i.e. a list of tokens that are going to be replaced when the markup is actually used.

Note: I have no control over the markup, structure of the markup or format/naming of the tokens that are being replaced.

<markup>
    <element1 attributea="blah">@@value1@@</element1>
    <element2>@@value2@@</element2>
    <element3>
        <element3point1>@@value1@@</element3point1>
        <element3point2>@@value3@@</element3point2>
        <element3point3>apple</element3point3>
    <element3>
    <element4>pear</element4>
</markup>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2024-08-03 01:47:54

怎么样:

    var keys = new HashSet<string>();
    Regex.Replace(input, "@@[^@]+@@", match => {
        keys.Add(match.Value);
        return ""; // doesn't matter
    });
    foreach (string key in keys) {
        Console.WriteLine(key);
    }

这:

  • 不需要解析 xml(只是字符串操作),
  • 只包含唯一值(不需要返回包含我们不需要的重复项的 MatchCollection想要)

但是,它可能会构建一个更大的字符串,所以可能只是匹配

var matches = Regex.Matches(input, "@@[^@]+@@");
var result = matches.Cast<Match>().Select(m => m.Value).Distinct();
foreach (string s in result) {
    Console.WriteLine(s);
}

How about:

    var keys = new HashSet<string>();
    Regex.Replace(input, "@@[^@]+@@", match => {
        keys.Add(match.Value);
        return ""; // doesn't matter
    });
    foreach (string key in keys) {
        Console.WriteLine(key);
    }

This:

  • doesn't bother parsing the xml (just string manipulation)
  • only includes the unique values (no need to return a MatchCollection with the duplicates we don't want)

However, it may build a larger string, so maybe just Matches:

var matches = Regex.Matches(input, "@@[^@]+@@");
var result = matches.Cast<Match>().Select(m => m.Value).Distinct();
foreach (string s in result) {
    Console.WriteLine(s);
}
贱人配狗天长地久 2024-08-03 01:47:54

我用你的示例编写了一个快速程序,这应该可以解决问题。

class Program
    {
        //I just copied your stuff to Test.xml
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load("Test.xml");
            var verbs=new Dictionary<string,string>();
            //Add the values to replace ehre
            verbs.Add("@@value3@@", "mango");
            verbs.Add("@@value1@@", "potato");
            ReplaceStuff(verbs, doc.Root.Elements());
            doc.Save("Test2.xml");
        }

        //A simple replace class
        static void ReplaceStuff(Dictionary<string,string> verbs,IEnumerable<XElement> elements)
        {
            foreach (var e in elements)
            {
                if (e.Elements().Count() > 0)
                    ReplaceStuff(verbs, e.Elements() );
                else
                {
                    if (verbs.ContainsKey(e.Value.Trim()))
                        e.Value = verbs[e.Value];
                }
            }
        }
    }

I wrote a quick prog with your sample, this should do the trick.

class Program
    {
        //I just copied your stuff to Test.xml
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load("Test.xml");
            var verbs=new Dictionary<string,string>();
            //Add the values to replace ehre
            verbs.Add("@@value3@@", "mango");
            verbs.Add("@@value1@@", "potato");
            ReplaceStuff(verbs, doc.Root.Elements());
            doc.Save("Test2.xml");
        }

        //A simple replace class
        static void ReplaceStuff(Dictionary<string,string> verbs,IEnumerable<XElement> elements)
        {
            foreach (var e in elements)
            {
                if (e.Elements().Count() > 0)
                    ReplaceStuff(verbs, e.Elements() );
                else
                {
                    if (verbs.ContainsKey(e.Value.Trim()))
                        e.Value = verbs[e.Value];
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文