Hi,
I need to generate an XML doc which looks something like this:
<Report xmlns="http://ReportSchema" xmlns:rd="http://DesignerSchema">
<Width>8in</Width>
<rd:DefaultName>report1</rd:DefaultName>
</Report>
When I call CreateNode for the 'Report' node, I pass in the ReportSchema for the URI, but I am not sure what I can do to get the second namespace in there as well.
Also, when I create the rd:DefaultName node(using CreateNode), I specify the namespace DesignerSchema, but the namespace is applied for that node only (as expected). But what I want is to specify the namespaces once at the top and use the alias names for the different nodes. How can I achieve it
Any help is greatly appreciated.
Thanks
Raghu

How to create XMLDocument using code that has multiple namespaces
fhunter
1. really for the above code, the output is
<Report xmlns:rd="http://DesignerSchema" xmlns="http://ReportSchema">
<Width>8in</Width>
<rd:DefaultName>Report1</rd:DefaultName>
</Report>
mark sure the prefix is the same in your code. If that already is the case, show your code
2. you need to use a different encoding when outputting, for example
XmlDocument doc = new XmlDocument();
...
System.IO.StreamWriter sw = new System.IO.StreamWriter("TestXmlForum.xml", false, System.Text.Encoding.UTF8);
doc.Save(sw);
sw.Close();
Bimlesh
you could add an xmlns attribute on the report node, for example
report.SetAttribute("xmlns:rd","http://DesignerSchema");
for example,
XmlDocument doc = new XmlDocument();
XmlElement report = doc.CreateElement("","Report","http://ReportSchema");
doc.AppendChild(report);
report.SetAttribute("xmlns:rd","http://DesignerSchema");
XmlElement width = doc.CreateElement("","Width","http://ReportSchema");
report.AppendChild(width);
width.InnerText = "8in";
XmlElement df = doc.CreateElement("rd","DefaultName","http://DesignerSchema");
report.AppendChild(df);
df.InnerText = "Report1";
doc.Save(Console.Out);
p030037
Thanks Xuegen, that did the trick. However, I'm still facing two more issues:
1. When the XML is spit out, the DefaultName still has its own namespace spec added (even though the namespace is now specified at the root thanks to your suggestion) how to change it so that the namespace specified at the root is used for child nodes which have alias 'rd'
2. Also, I specify the UTF-encoding in the XmlDocument class using the CreateXMLDeclaration("1.0", "utf-8", null) but this does does not get spit out when I use XmlDocument.WriteTo method. Any suggestions on how to get the UTF encoding string at the top if the XML output by XMLDocument
Thanks and appreciate the help.
Raghu
DLASKEY
Thanks for the reply Xuegen. You're right about #1. I think I overlooked something else. I tried again with sample code and it works as expected.
Thanks
Raghu