开发者

Creating xml in c#

开发者 https://www.devze.com 2023-02-09 22:09 出处:网络
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.Emp开发者_如何学Goty.My element names contains colon ex gd:City.The pro

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.Emp开发者_如何学Goty.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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消