473,804 Members | 4,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Create Element("FirstE lement");
myxmlfile.SetAt tribute("xmlns" , "http://www.server.com/XMLSchema");
myxmlfile.SetAt tribute("xmlns: xs", "http://www.w3.org/2001/XMLSchema-
instance");
myxmlfile.SetAt tribute("xs:sch emaLocation", "http://www.server.com/
XMLSchema myxml.xsd");
document.Append Child(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:schemaLocati on="http://
www.server.com/XMLSchema myxml.xsd">

Specifically, my problem is with line,
myxmlfile.SetAt tribute("xs:sch emaLocation", "http://www.server.com/
XMLSchema myxml.xsd");

When this line is excuted the xs:schemaLocati on, 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 9043
nygiantswin2005 wrote:
XmlDocument document = new XmlDocument();
XmlElement myxmlfile = document.Create Element("FirstE lement");
myxmlfile.SetAt tribute("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.Create Element("FirstE lement",
"http://www.server.com/XMLSchema");
myxmlfile.SetAt tribute("xmlns: xs", "http://www.w3.org/2001/XMLSchema-
instance");
myxmlfile.SetAt tribute("xs:sch emaLocation", "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.Create Element("FirstE lement",
"http://www.server.com/XMLSchema");
XmlAttribute schemaLocation = document.Create Attribute("xsi" ,
"schemaLocation ", xsi);
schemaLocation. Value = "http://www.server.com/XMLSchema myxml.xsd";
myxmlfile.SetAt tributeNode(sch emaLocation);
document.Append Child(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
3537
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 cannot have ':' in it. However, MSDN usage doc for this method explicitly says that ':' will be parsed and the prefix would be retrieved.
4
3879
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 attributes and child nodes. In .NET, the type of XA1 is xml.xmlelement. If I use clone method. The copy of XA1 is belong to XA. I can not append it to XB. If I careate a new instance from XB.CreateElement method. I need to copy every attribute of...
0
1546
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, Brown) and two sizes (Large, Small). My algorithm will run through a special XML tree (not a regular tree,
2
7252
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 = controlDocument.CreateElement( "ns0:MyRootElement" ); rootNode.SetAttribute( "xmlns:ns0", "http://my.namespace.uri" ); controlDocument.AppendChild(rootNode); I'm trying to get it so that it comes out as follows:
4
3611
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 td.setAttribute("myAttr", "hello"); Could somebody explain the difference between these two?
5
2003
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 couldn't be anything wrong with the copy. When the user clicks on 'Click here', the function SwitchSheets should switch the stylesheets: document.all.SetAttribute("HREF", "BigStyle.css",0);
4
5057
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 value then put it back as the oncontextmenu attribute. Here's an example page if you want to try it out... <html> <head> <titletesting </title> <script><!--
1
1508
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 elements by just using a method wrapped around the element itself rather than using if statements. Could someone helpme out with the syntax so that the method returns back the object please? thanks in advance, sean this.someNode...
0
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.