XmlElement SetAttribute method not working correctly

N

nygiantswin2005

Hi

I have wrote a method in my C# class with the following code, to
create an XMl file.
The root element is suppose to have 3 attibutes. I can not get
attributes to appear correctly.



XmlDocument document = new XmlDocument();
XmlElement myxmlfile = document.CreateElement("FirstElement");
myxmlfile.SetAttribute("xmlns", "http://www.server.com/XMLSchema");
myxmlfile.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema-
instance");
myxmlfile.SetAttribute("xs:schemaLocation", "http://www.server.com/
XMLSchema myxml.xsd");
document.AppendChild(myxmlfile);



This is how the XML document prints out when I compile and run the
code..
<?xml version="1.0" encoding="utf-8"?>
<FirstElement xmlns="http://www.server.com/XMLSchema" xmlns:xs="http://
www.w3.org/2001/XMLSchema-instance" schemaLocation="http://
www.server.com/XMLSchema myxml.xsd">

But I need to make it look like this.
<?xml version="1.0" encoding="utf-8"?>
<FirstElement xmlns="http://www.server.com/XMLSchema" xmlns:xs="http://
www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://
www.server.com/XMLSchema myxml.xsd">

Specifically, my problem is with line,
myxmlfile.SetAttribute("xs:schemaLocation", "http://www.server.com/
XMLSchema myxml.xsd");

When this line is excuted the xs:schemaLocation, becomes
schemaLocation. The "xs:" is being dropped.

What do you do to fix this problem.
why doesn't the SetAttribute() method work correctly?
What do have to do correct this problem.

thanks,
nitadmin
 
M

Martin Honnen

nygiantswin2005 said:
XmlDocument document = new XmlDocument();
XmlElement myxmlfile = document.CreateElement("FirstElement");
myxmlfile.SetAttribute("xmlns", "http://www.server.com/XMLSchema");

That is plain wrong, you need to pass the namespace URI to the
CreateElement method e.g.
XmlElement myxmlfile = document.CreateElement("FirstElement",
"http://www.server.com/XMLSchema");
myxmlfile.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema-
instance");
myxmlfile.SetAttribute("xs:schemaLocation", "http://www.server.com/
XMLSchema myxml.xsd");

You do not need to create the xmlns declarations, simply create the
attributes you need e.g.

const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
XmlDocument document = new XmlDocument();
XmlElement myxmlfile = document.CreateElement("FirstElement",
"http://www.server.com/XMLSchema");
XmlAttribute schemaLocation = document.CreateAttribute("xsi",
"schemaLocation", xsi);
schemaLocation.Value = "http://www.server.com/XMLSchema myxml.xsd";
myxmlfile.SetAttributeNode(schemaLocation);
document.AppendChild(myxmlfile);

the serializer will then add all namespace declarations as needed based
on the qualified names of elements and attributes.

And be aware that any children and descendants of the root element need
to be created too with
CreateElement("foo", "http://www.server.com/XMLSchema")
if you want them in the same namespace as the root element.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top