2

我正在尝试将所有“CSS”元素“value”的值设置为“”,这是代码:

XDocument doc = XDocument.Load(fi.FullName);
XNamespace rep = "http://developer.cognos.com/schemas/report/8.0/";

List<XElement> cssElements =
    (from e in doc.Root.DescendantsAndSelf(rep + "CSS")
     where
     (
         (e.Attribute("value") != null)
     )
     select e).ToList();

//modify Attribute in elements
foreach (XElement xe in cssElements)
{
    xe.Attribute("value").Value = "";
}

但是,我不想修改这个 CSS,它的祖先是“crosstab”和“style”(下面的 xml):

<crosstab name="Crosstab1" refQuery="Query1">
<crosstabSuppress type="rows"/>
<style>
    <CSS value="border-collapse:collapse;font-family:'Times New Roman'"/>  

我怎样才能做到这一点?谢谢!

4

1 回答 1

1

如果我理解正确,可能是这样的:

...

List<XElement> cssElements =
    (from e in doc.Root.DescendantsAndSelf(rep + "CSS")
        where
        (
            (e.Attribute("value") != null) && !(e.Ancestors(rep + "style").Any() && e.Ancestors(rep + "crosstab").Any())
        )
        select e).ToList();

...

我只将此添加到您的where-clause 中:

&& !(e.Ancestors(rep + "style").Any() && e.Ancestors(rep + "crosstab").Any())
于 2012-05-03T16:19:04.093 回答