NameSpace attribues

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

Loading an xml as below and adding a namespace attribute.

XmlDocument DocXml = new XmlDocument();
DocXml.LoadXml("<DA/>");
XmlElement Felem = DocXml.CreateElement("F");
Felem.SetAttribute("ABC", "1");
Felem.SetAttribute("CT:XYZ", "-1");
Felem.SetAttribute("xmlns:CT", "MyNameSpace");
DocXml.DocumentElement.AppendChild(Felem);
MessageBox.Show(DocXml.OuterXml);

Here namepace attributes are missing in outerXML

whts bet way to do this...
I want to load the outerXml again like this...


XmlDocument NewDocXml = new XmlDocument();
NewDocXml.LoadXml(DocXml.OuterXml);

this fails

praveen
 
Hi Praveen,

I'm sorry I'm not sure whether I have understood your issue completely. I
think you want to get the outxml such as
<DA><F ABC="1" xmlns:CT="myNameSpace1" CT:XYZ="111" /></DA>

If this is true, you can do the following code to achieve this. Also,
please don't hesitate to correct me if I have misunderstood anything here.

XmlDocument DocXml = new XmlDocument();
DocXml.LoadXml("<DA/>");
XmlElement Felem = DocXml.CreateElement("F");
Felem.SetAttribute("ABC", "1");
Felem.SetAttribute("xmlns:CT", "myNameSpace1");
Felem.SetAttribute("XYZ", "myNameSpace1", "111");
DocXml.DocumentElement.AppendChild(Felem);
MessageBox.Show(DocXml.OuterXml);

XmlDocument NewDocXml = new XmlDocument();
NewDocXml.LoadXml(DocXml.OuterXml);

Hope this helps,
Sincerely,
Wen Yuan
 
Hi Praveen,

Just want to check if the issue has been resolved?
If it still persists, please don't hesitate to update here.
We'll go on to assist you on it. Thanks.

Have a great day,
Sincerely,
Wen Yuan
 
Back
Top