开发者

regular expression conditional problems in C#

I have some content nested in span tags. Some of them have details I need to pull, and some do not. I can't figure out how to check for two options and pull the proper data. These groups repeat. For example:

<span name="foo">
    <span name="bar">
        Missing Data
    </span>
</span>
<span name="foo">
    <span name="bar">
        <span name="detail1">first detail</span>
        <span name="detail2">second detail</span>
开发者_StackOverflow社区    </span>
</span>

I have to capture the details individually if they are there, otherwise I need to set those values to null in the strings in my program when looping through the matchcollection so my code needs to set strDetail1 and strDetail2 to "" or the values "first detail" and "second detail" if that makes sense.


I suggest using XPath to parse values. For parsing xml structure this will be more reliable than Regex.

var xml = @"
    <root>
    <span name=""foo"">
        <span name=""bar"">
            Missing Data
        </span>
    </span>
    <span name=""foo"">
        <span name=""bar"">
            <span name=""detail1"">first detail</span>
            <span name=""detail2"">second detail</span>
        </span>
    </span>
    </root>
";

var document = XDocument.Parse(xml);
var details = document.XPathSelectElements("//span[@name='foo']/span[@name='bar']/span[starts-with(@name,'detail')]")
    .Select(arg => arg.Value)
    .ToList();

or LINQ-to-XML

var details = document
    .Descendants("span").Where(arg => arg.Attribute("name").Value == "foo")
    .Elements("span").Where(arg => arg.Attribute("name").Value == "bar")
    .Elements("span").Where(arg => arg.Attribute("name").Value.StartsWith("detail"))
    .Select(arg => arg.Value)
    .ToList();

[Edit] I might misunderstand the question. Seems like you also want to replace or fill some values. You can do this with above-mentioned approach as long as you have XDocument. For example this code will clear values of the detail1 and detail2 elements:

var detailNodes = document.XPathSelectElements("//span[@name='foo']/span[@name='bar']/span[starts-with(@name,'detail')]")
    .ToList();

detailNodes[0].Value = string.Empty;
detailNodes[1].Value = string.Empty;

var newXml = document.ToString();

[Edit]

How to add an element:

var elementsWithMissingDetals = document
    .XPathSelectElements("//span[@name='foo']/span[@name='bar' and count(*)=0]")
    .ToList();

foreach (var elementsWithMissingDetal in elementsWithMissingDetals)
{
    elementsWithMissingDetal.Add(
        new XElement("span", "first detail", new XAttribute("name", "detail1")));
    elementsWithMissingDetal.Add(
        new XElement("span", "second detail", new XAttribute("name", "detail2")));
}

var newXml = document.ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜