user3165438
user3165438

Reputation: 2661

Set namespace to XElement

I get an empty xmlns="" attribute in XElemenet in an XML tree. When I set its namespace to the document namespace, like this:

string xmlns="FreeForm/SchemaDescription";
XNamespace ana = xmlns;
XElement interactiveRootTag = new XElement(ana + "InteractiveRootTag");

the empty xmlns="" is not exist any more, but all of the children of this XElement, get the empty xmlns="".

Any ideas?

Upvotes: 0

Views: 829

Answers (1)

Rashmin
Rashmin

Reputation: 86

You have to add the child element in this way:

string xmlns="FreeForm/SchemaDescription";

XNamespace ana = xmlns;

XElement interactiveRootTag = new XElement(ana + "InteractiveRootTag");
interactiveRootTag.Add(new XElement(ana + "ChildElement", 
    new XAttribute("attribute","AttributeValue")));

To get XML like this

<InteractiveRootTag xmlns="FreeForm/SchemaDescription">
   <ChildElement attribute="AttributeValue" />
</InteractiveRootTag>

Upvotes: 1

Related Questions