471,348 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

XmlElement SetAttribute method not working correctly

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
Jun 27 '08 #1
2 8838
nygiantswin2005 wrote:
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.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Martin,

Thank you for your assistance.

nitadmin
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by nibble | last post: by
4 posts views Thread by Steven.Xu | last post: by
2 posts views Thread by Christoph | last post: by
4 posts views Thread by mitch | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.