2

why do I get here the error message: Doubled Attribute? I guess this means, there are more than one Attributes with the same name and value.

 XElement XMLRun = new XElement("RessourceAttribute");

 foreach (var kvp in Run) //kvp = KeyValuePair<string,string> and Run = List<KeyValuePair<string, string>>
 {
    XAttribute aKey = new XAttribute("name", kvp.Key);
    XAttribute aValue = new XAttribute("value", kvp.Value);
    XMLRun.Add(aKey, aValue);       
 }

 XMLE.Add(XMLRun);

On every step there should be this two new Attributes generated and then added on the parent node. I am pretty sure the problem is here, because the Attributenames must be different. The error occurs on my Enterprise Architect AddIn. Is there a possibility that the new generated Attributes have different names?

4

2 回答 2

3

好吧,如果Run列表是这样的:

"foo": "bar"
"foo2": "bar2"

你正在做这样的事情:

<ResourceAttribute name="foo" value="bar" name="foo2" value="bar2" />

这确实是非法的,因为一个属性出现了两次......

你想做什么?

于 2013-09-17T15:26:32.167 回答
2

发现如果Run包含多于一对,您将收到错误似乎相当简单。鉴于:

var Run = new Dictionary<string, string>
{
    { "1", "a" },
    { "2", "b" },
    { "3", "c" },
};

然后,您的代码将生成无效的 XML:

<ResourceAttribute name="1" value="a"
                   name="2" value="b"
                   name="3" value="c" />

很难说没有一些示例 XML。也许你的意思是有很多<ResourceAttribute ...>元素?

foreach (var pair in Run)
{
    XMLE.Add(
        new XElement("ResourceAttribute",
            new XAttribute("key", pair.Key),
            new XAttribute("value", pair.Value)
        )
    );
}

还是您的意思是将这些属性放在子元素上?

    // replace original loop body
    XMLRun.Add(new XElement("Run", XMLRunAttributeK, XMLRunAttributeV));

还是根据名称直接命名属性?

    // replace original loop body
    XMLRun.Add(new XAttribute(pair.Key, pair.Value));
于 2013-09-17T15:26:00.177 回答