Cannot get regular expression work correctly with multiline

前端 未结 4 2230
南笙
南笙 2020-12-15 06:16

I have a quite big XML output from an application. I need to process it with my program and then feed it back to the original program. There are pieces in this XML which nee

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 07:03

    RegExp is a poor tool for xml... could you not juts load it into an XDocument / XmlDocument and use xpath? If you clarify the modifications you want to make, I expect we can fill in the blanks... namespaces are probably the main thing to make it complex in this case, so we just need to use an XmlNamespaceManager.

    Here's an example that is, granted, more complex than just a regex - however, I would expect it to cope a lot better with the nuances of xml:

        string xml = @"
    value
    here are some other tags
    value
    ";
    
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
        mgr.AddNamespace("sys", "foobar");
        var matches = doc.SelectNodes("//sys:customtag[@sys:type='Processtart']", mgr);
        foreach (XmlElement start in matches)
        {
            XmlElement end = (XmlElement) start.SelectSingleNode("following-sibling::sys:customtag[@sys:type='Procesend'][1]",mgr);
            XmlNode node = start.NextSibling;
            while (node != null && node != end)
            {
                Console.WriteLine(node.OuterXml);
    
                node = node.NextSibling;
            }
        }
    

提交回复
热议问题