2

Hi I am creating an xml programatically.I am using namespace.In some places i dont need the namespace and so i am passing String.Empty.My element names contains colon ex gd:City.The problem is if i am passing null in the third parameter of createElement then in output i am getting City and not gd:city.How do i solve this problem.I need the o/p as gd:City at the same time i dont want to pass the namespace.

Regards Sanchaita Chakraborty

2
  • 1
    If you prefix an element with gd: then you are using the gd namespace Commented Feb 10, 2011 at 10:53
  • 1
    So you want namespace but don't want to pass it yourself? How about a wrapper method that will add the namespace for you? Commented Feb 10, 2011 at 10:56

1 Answer 1

1

You need to use NameSpaceManager. Something like:

XmlNamespaceManager nsm = new XmlNamespaceManager(myXmlDocument.NameTable);
nsm.AddNamespace("gd", "http://mynamespacehere");
XmlNode nde = myXmlDocument.CreateElement("gd", "NewElement", "http://mynamespacehere");

Edit : As per the other poster's comments, you can't create an element name containing a colon (see the W3Spec here and a tut on namespaces here). If the element HAS a colon (:), it means that you are ALREADY using a namespace - look for a corresponding xmlns:gd="http://mynamespacehere" in a parent node of your City element (or City itself).

"gd" is just a placeholder (called a prefix) for the namespace. Your element is City, in a namespace - I don't have your complete XML message - for which the prefix "gd" was assigned.

Sign up to request clarification or add additional context in comments.

1 Comment

In some cases i want to keep the namespace as null.But on doing this i am losing the prefix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.