473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 9010
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nibble | last post by:
I'm doign the following: XmlElement myElem....; string name = "xmlns:"+"abc"; string value = "http://www.abc.com"; myElem.SetAttribute(name, value); I get an exception saying that name...
4
by: Steven.Xu | last post by:
Hello everyone. I have two instance of xml.xmldocument. One is XA another is XB. XA has a child node XA1. XA1 has many attributes and child nodes. Now I want to copy XA1 to XB with all of it's...
0
by: ryanr | last post by:
I've run into a problem with a recursive algorithm. Basically, it grabs a tree, and creates premutations based off it. For example: Suppose you have you sell shoes in two colors (Black,...
2
by: Christoph | last post by:
I'm using the following code to create what will be the root element of my XML document. XmlDocument controlDocument = new XmlDocument(); //add root node XmlElement rootNode =...
4
by: mitch | last post by:
Suppose I have a DOM element, say a td, and I want to add a value to it to be used later. I am unclear on when it's OK to do td.myAttr = "hello"; versus when I need to do ...
5
by: RC | last post by:
Please look at this page: http://www.norbertverboeket.com/test There are 2 stylesheets, StyleSheet.css and BigStyle.css. One is copied from the other. Only the font-size is changed, so there...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
1
by: bullrout | last post by:
Hi There, I have a method which uses some values passed in as xml, at times some of the elements in the document will be null. The document itself has many elements and I wanted to handle the null...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.