Hi,
I have a xml document displayed below, which I want to programmatically create.
<rootABC xmlns="http://www.xyz.space.com/land">
<ele1 xmlns:xsd="http://www.xyz.space.com/Airschemas"
xmlns:xsi="http://www.xyz.space.com/AirschemaInst">
<ObjVal>
<Name>POC2</Name>
<Age>tempCube</Age>
<Height>dimension1</Height>
<Weight>MeasureId1</Weight>
<color>Partition1</color>
</ObjVal>
<country>ProcessFull</country>
<Airport>UseExisting</Airport>
</ele1>
</rootABC>
to create the xml document I used:
XmlDocument xdoc = new XmlDocument();
XmlElement rootABC;
XmlElement ele1;
rootABC = xdoc.CreateElement("rootABC","http://www.xyz.space.com/land");
xdoc.AppendChild(rootABC);
Now when I try to create the second element "ele1" with the two namespaces, I am not able to do it. I tried giving both of them in the same namespace uri, but then it generates wrong xml.
Also, how do i create elements Like the "ObjVal" without any namespace uri's because the createElement does not allow withut a namespace uri and it generates the namespace attribute with blanks.
Please give any info with code sample.
URGENT.
thanks and regards

Blank XMLNS="" gets inserted? ::URGENT::
mewdied
The output I see is:
< xml version="1.0" encoding="cp866" >
<rootABC xmlns="http://www.xyz.space.com/land">
<ele1 xmlns:xsd="http://www.xyz.space.com/Airschemas" xmlns:xsi="http://www.xyz.space.com/AirschemaInst">
<ObjVal>
<Name>POC2</Name>
</ObjVal>
</ele1>
</rootABC>
There is no any xmlns="".
Please post exect repro and its results.
Terry125
Samples are bellow.
As you can see populating XmlDocument with XmlWriter.Write*() methods is much simpler.
string myNS = "http://www.xyz.space.com/land";
// Populating XmlDocument using DOM API:
XmlDocument xdoc1 = new XmlDocument();
XmlElement rootABC = xdoc1.CreateElement("rootABC", myNS);
xdoc1.AppendChild(rootABC);
XmlElement ele1 = xdoc1.CreateElement("ele1", myNS);
rootABC.AppendChild(ele1);
XmlAttribute xsd = xdoc1.CreateAttribute("xmlns", "xsd", "http://www.w3.org/2000/xmlns/");
xsd.Value = "http://www.xyz.space.com/Airschemas";
ele1.Attributes.Append(xsd);
XmlAttribute xsi = xdoc1.CreateAttribute("xmlns", "xsi", "http://www.w3.org/2000/xmlns/");
xsi.Value = "http://www.xyz.space.com/AirschemaInst";
ele1.Attributes.Append(xsi);
XmlElement ObjVal = xdoc1.CreateElement("ObjVal", myNS);
ele1.AppendChild(ObjVal);
XmlElement Name = xdoc1.CreateElement("Name", myNS);
ObjVal.AppendChild(Name);
Name.AppendChild(xdoc1.CreateTextNode("POC2"));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter w1 = XmlWriter.Create(Console.Out, settings);
xdoc1.WriteTo(w1);
Console.WriteLine();
Console.WriteLine();
// Populating XmlDocument using XPathNavigator API:
XmlDocument xdoc2 = new XmlDocument();
XmlWriter docWriter = xdoc2.CreateNavigator().AppendChild();
docWriter.WriteStartElement("rootABC", myNS);
docWriter.WriteStartElement("ele1", myNS);
docWriter.WriteAttributeString("xmlns", "xsd", "http://www.w3.org/2000/xmlns/", "http://www.xyz.space.com/Airschemas");
docWriter.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", "http://www.xyz.space.com/AirschemaInst");
docWriter.WriteStartElement("ObjVal", myNS);
docWriter.WriteElementString("Name", myNS, "POC2");
docWriter.WriteEndElement();
docWriter.WriteEndElement();
docWriter.WriteEndElement();
docWriter.Close();
XmlWriter w2 = XmlWriter.Create(Console.Out, settings);
xdoc2.WriteTo(w2);
Console.WriteLine();
Console.WriteLine();
w2.Flush();
jkotas
Hi,
Thanks for the help. The samples were a great help.
I used the XML DOM method, (Just for consistency in my code :-)). but this created a small problem which I am not able to solve.
according to your code: the XML I get is almost perfect except for a blank XMLNS="" attribute that gets inserted with the XmlElement ele1. Also infact I could not set the "myNS" for "ele1". what I mean is the code: XmlElement ele1 = xdoc1.CreateElement("ele1", myNS);
this gives an error saying: cannot bind to ........ namespace. so I removed the myNS from it and did the same for the others. all the other nodes did not give a problem, but the ele1 Element adds a blank xmlns="" with the other two added attributes.
I tried removing the xmlns using the attributes.removeat() method, but it does not get removed. How to work around this problem
I was also thinking one more thing. Can I not have a template XML that resembles the xml that I need without the data/information in it. Then I create a xml document out of the xml template and then insert the data/ info that I need. wont this be easier Is this a correct way of working with a requirement to create the same xml document with different data
Thanks for the help
Regards