user3529183
user3529183

Reputation: 87

Secondary xmlns on the child element of my XML

I am creating an xml file and trying to append the Namespace to the root Node.

    doc = new XDocument(new XElement(XName.Get("urlset",  "http://www.sitemaps.org/schemas/sitemap/0.9"),

However, when I do this it appends an empty namespace on the next node, the child node of the XDocument?

Upvotes: 1

Views: 490

Answers (1)

decPL
decPL

Reputation: 5402

Further XElements will have an empty namespace as you haven't specified any namespace for them. This then needs to be inidicated on child elements as it differs from the parent namespace. Unfortunately you need to specify the same namespace for all child documents if you want to "fix" this.

Luckily there's a shorthand code to do this:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";

Then initialize all your elements like so:

new XElement(ns + ELEMENT_NAME, ...);

e.g.:

new XElement(ns + "urlset", ...);

instead of XName.Get.

Upvotes: 1

Related Questions