【问题标题】:XElement add styleXElement 添加样式
【发布时间】:2013-04-04 11:52:56
【问题描述】:

我想拥有一个风格如下的 XElement:

<g style="fill-opacity:0.7">

所以我这样做了:

XElement root = new XElement("g");
root.SetAttributeValue("style",
                from attributes in Child.Attributes
                where char.IsUpper(attributes.Key[0]) & !attributes.Value.ToString().StartsWith(transformNS)
                select new XAttribute("style", attributes.Key + ":" + attributes.Value));

但我拥有的是

<g style="System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Collections.Generic.KeyValuePair`2[System.String,System.Object],System.Xml.Linq.XAttribute]">

有人可以帮助我吗?

最好的问候

【问题讨论】:

  • 您是否尝试选择单个子属性?
  • 可能不止一个
  • 那么你想如何组合它们呢?您的代码不包含任何执行此操作的内容。
  • linq 会将所有属性放在样式中

标签: c# asp.net xelement


【解决方案1】:

那是因为您选择了一个完整的 XAttribute 作为属性的值。 您需要遍历您的属性并将它们合并成一个字符串。然后使用这个字符串作为你的属性的值。

类似这样的:

XElement root = new XElement("g");
IEnumerable<string> styleprops = from attributes in Child.Attributes
                                 where char.IsUpper(attributes.Key[0]) & !attributes.Value.ToString().StartsWith(transformNS)
                                 select attributes.Key + ":" + attributes.Value

string value = string.empty;

foreach(string prop in styleprops){
    value += prop + ";";
}

root.SetAttributeValue("style", value);

【讨论】:

  • 好的,很乐意提供帮助。 (如果解决了您的问题,请将回复标记为答案)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多