473,385 Members | 1,863 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,385 software developers and data experts.

Adding attributes to a node

I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>

However I am unclear on how to add these types of attributes. The following
doesn't seem to work.

Scott

XmlNode attribute;
attribute = doc.CreateNode(XmlNodeType.Attribute, "xsi:type", "");
attribute.Value = "Group";
grantee.Attributes.SetNamedItem(attribute);

attribute = doc.CreateNode(XmlNodeType.Attribute, "xmlns:xsi", "");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.SetNamedItem(attribute);
Oct 20 '08 #1
11 2268
M1iS wrote:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>

However I am unclear on how to add these types of attributes. The following
doesn't seem to work.

Scott

XmlNode attribute;
attribute = doc.CreateNode(XmlNodeType.Attribute, "xsi:type", "");
attribute.Value = "Group";
grantee.Attributes.SetNamedItem(attribute);

attribute = doc.CreateNode(XmlNodeType.Attribute, "xmlns:xsi", "");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.SetNamedItem(attribute);
instead of doing grantee.Attributes.SetNamedItem(attribute);
do grantee.Attributes.append(attribute);
Oct 20 '08 #2
M1iS wrote:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>

However I am unclear on how to add these types of attributes. The following
doesn't seem to work.

Scott

XmlNode attribute;
attribute = doc.CreateNode(XmlNodeType.Attribute, "xsi:type", "");
attribute.Value = "Group";
grantee.Attributes.SetNamedItem(attribute);

attribute = doc.CreateNode(XmlNodeType.Attribute, "xmlns:xsi", "");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.SetNamedItem(attribute);
Sorry also instead of doc.CreateNode, it should be doc.CreateAttribute()
Oct 20 '08 #3
Okay I tried the following:

XmlAttribute attribute;

attribute = doc.CreateAttribute("xmlns:xsi");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.Append(attribute);

attribute = doc.CreateAttribute("xsi:type");
attribute.Value = "Group";
grantee.Attributes.Append(attribute);

It gives me:
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="Group" />

Close, but not quite there.

"Navid" wrote:
M1iS wrote:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>

However I am unclear on how to add these types of attributes. The following
doesn't seem to work.

Scott

XmlNode attribute;
attribute = doc.CreateNode(XmlNodeType.Attribute, "xsi:type", "");
attribute.Value = "Group";
grantee.Attributes.SetNamedItem(attribute);

attribute = doc.CreateNode(XmlNodeType.Attribute, "xmlns:xsi", "");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.SetNamedItem(attribute);
Sorry also instead of doc.CreateNode, it should be doc.CreateAttribute()
Oct 20 '08 #4
M1iS wrote:
Okay I tried the following:

XmlAttribute attribute;

attribute = doc.CreateAttribute("xmlns:xsi");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.Append(attribute);

attribute = doc.CreateAttribute("xsi:type");
attribute.Value = "Group";
grantee.Attributes.Append(attribute);

It gives me:
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="Group" />

Close, but not quite there.

"Navid" wrote:
>M1iS wrote:
>>I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>

However I am unclear on how to add these types of attributes. The following
doesn't seem to work.

Scott

XmlNode attribute;
attribute = doc.CreateNode(XmlNodeType.Attribute, "xsi:type", "");
attribute.Value = "Group";
grantee.Attributes.SetNamedItem(attribute);

attribute = doc.CreateNode(XmlNodeType.Attribute, "xmlns:xsi", "");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.SetNamedItem(attribute);
Sorry also instead of doc.CreateNode, it should be doc.CreateAttribute()
You need to user the CreateAttribute overload that is String, nmspace,
the namespace for Type is xsi so you need to put the name space as
"http://www.w3.org/2001/XMLSchema-instance"

so
attribute=doc.CreateAttribute("xsi:type","http://www.w3.org/2001/XMLSchema-instance");
attribute.Value = "Group";
grantee.Attributes.Append(attribute);

that should work.
Oct 20 '08 #5
Thanks so much, that did the trick.
"Navid" wrote:
M1iS wrote:
Okay I tried the following:

XmlAttribute attribute;

attribute = doc.CreateAttribute("xmlns:xsi");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.Append(attribute);

attribute = doc.CreateAttribute("xsi:type");
attribute.Value = "Group";
grantee.Attributes.Append(attribute);

It gives me:
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="Group" />

Close, but not quite there.

"Navid" wrote:
M1iS wrote:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI>
</Grantee>

However I am unclear on how to add these types of attributes. The following
doesn't seem to work.

Scott

XmlNode attribute;
attribute = doc.CreateNode(XmlNodeType.Attribute, "xsi:type", "");
attribute.Value = "Group";
grantee.Attributes.SetNamedItem(attribute);

attribute = doc.CreateNode(XmlNodeType.Attribute, "xmlns:xsi", "");
attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
grantee.Attributes.SetNamedItem(attribute);
Sorry also instead of doc.CreateNode, it should be doc.CreateAttribute()

You need to user the CreateAttribute overload that is String, nmspace,
the namespace for Type is xsi so you need to put the name space as
"http://www.w3.org/2001/XMLSchema-instance"

so
attribute=doc.CreateAttribute("xsi:type","http://www.w3.org/2001/XMLSchema-instance");
attribute.Value = "Group";
grantee.Attributes.Append(attribute);

that should work.
Oct 20 '08 #6
M1iS wrote:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
xmlns attributes are in the namespace http://www.w3.org/2000/xmlns/ so
you need e.g.
const string xmlns = "http://www.w3.org/2000/xmlns/";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";

XmlDocument doc = new XmlDocument();
XmlElement grantee = doc.CreateElement("Grantee");

XmlAttribute xsiNs = doc.CreateAttribute("xmlns", "xsi", xmlns);
xsiNs.Value = xsi;
grantee.SetAttributeNode(xsiNs);

XmlAttribute xsiType = doc.CreateAttribute("xsi", "type", xsi);
xsiType.Value = "Group";
grantee.SetAttributeNode(xsiType);

doc.AppendChild(grantee);

doc.Save(Console.Out);
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 21 '08 #7
Okay, that works as well, I have one small issue though. I'm gettng an extra
xmlns="" attribute in my Grant node when it should not have this attribute.
For example:

<Grant xmlns="">
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>

Here is my entire block of code:

XmlDocument doc = new XmlDocument();
doc.LoadXml(SiteUtility.GetFileText("~/_dev/acl.xml"));

XmlNamespaceManager xmlNM = new XmlNamespaceManager(doc.NameTable);
xmlNM.AddNamespace("def", @"http://s3.amazonaws.com/doc/2006-03-01/");

XmlNode acl =
doc.SelectSingleNode("/def:AccessControlPolicy/def:AccessControlList", xmlNM);

XmlElement grant = doc.CreateElement("Grant");
XmlElement grantee = doc.CreateElement("Grantee");

XmlNode uri = doc.CreateNode(XmlNodeType.Element, "URI", "");
uri.InnerText = "http://acs.amazonaws.com/groups/global/AllUsers";

XmlNode permission = doc.CreateNode(XmlNodeType.Element,
"Permission", "");
permission.InnerText = "READ";

const string xmlns = "http://www.w3.org/2000/xmlns/";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";

XmlAttribute xsiNs = doc.CreateAttribute("xmlns", "xsi", xmlns);
xsiNs.Value = xsi;
grantee.SetAttributeNode(xsiNs);

XmlAttribute xsiType = doc.CreateAttribute("xsi", "type", xsi);
xsiType.Value = "Group";
grantee.SetAttributeNode(xsiType);

grant.AppendChild(grantee);
grant.AppendChild(permission);
grantee.AppendChild(uri);
acl.AppendChild(grant);

doc.Save("c:\\temp\\test.xml");
"Martin Honnen" wrote:
M1iS wrote:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node
(Grantee) like the following:

<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">

xmlns attributes are in the namespace http://www.w3.org/2000/xmlns/ so
you need e.g.
const string xmlns = "http://www.w3.org/2000/xmlns/";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";

XmlDocument doc = new XmlDocument();
XmlElement grantee = doc.CreateElement("Grantee");

XmlAttribute xsiNs = doc.CreateAttribute("xmlns", "xsi", xmlns);
xsiNs.Value = xsi;
grantee.SetAttributeNode(xsiNs);

XmlAttribute xsiType = doc.CreateAttribute("xsi", "type", xsi);
xsiType.Value = "Group";
grantee.SetAttributeNode(xsiType);

doc.AppendChild(grantee);

doc.Save(Console.Out);
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 22 '08 #8
M1iS wrote:
<Grant xmlns="">
XmlElement grant = doc.CreateElement("Grant");
XmlElement grantee = doc.CreateElement("Grantee");
Generally with the DOM API you need to create each element in the proper
namespace so if these elements are supposed to be in a certain namespace
then you need to create them in that namespace by passing in the
namespace URI to the CreateElement method e.g.
const string someNs = "http://example.com/2008/ex1";
XmlElement grant = doc.CreateElement("Grant", someNs);
XmlElement grantee = doc.CreateElement("Grantee", someNs);

Replace "http://example.com/2008/ex1" with the namespace URI of the
namespace you need these elements to be in. Or simply do
const string someNs = acl.NamespaceURI;
then it should be fine, assuming you want these elements to be in the
same namespace as the acl element node you select.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 22 '08 #9
But what if I don't want a namespace attribute at all on the grant node? I
want a node like this:

<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>

Rather than this:

<Grant xmlns="">
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>


"Martin Honnen" wrote:
M1iS wrote:
<Grant xmlns="">
XmlElement grant = doc.CreateElement("Grant");
XmlElement grantee = doc.CreateElement("Grantee");

Generally with the DOM API you need to create each element in the proper
namespace so if these elements are supposed to be in a certain namespace
then you need to create them in that namespace by passing in the
namespace URI to the CreateElement method e.g.
const string someNs = "http://example.com/2008/ex1";
XmlElement grant = doc.CreateElement("Grant", someNs);
XmlElement grantee = doc.CreateElement("Grantee", someNs);

Replace "http://example.com/2008/ex1" with the namespace URI of the
namespace you need these elements to be in. Or simply do
const string someNs = acl.NamespaceURI;
then it should be fine, assuming you want these elements to be in the
same namespace as the acl element node you select.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 22 '08 #10
M1iS wrote:
But what if I don't want a namespace attribute at all on the grant node? I
want a node like this:

<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>
I did not in any way suggest to put a namespace declaration attribute on
the Grant element, I did only suggest to pass in the namespace URI as a
parameter to the CreateElement method.

If you have markup of the form
<root xmlns="http://example.com/ns1"><foo><bar>baz</bar></foo></root>
then the namespace applies to all elements (e.g. the 'root' element, the
'foo' element, and the 'bar' element).

However if you want to create that structure with the DOM API then you
need to create each element in the namespace e.g.
const string ns = "http://example.com/ns1";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("root", ns);
XmlElement foo = doc.CreateElement("foo", ns);
XmlElement bar = doc.CreateElement("bar", ns);
bar.InnerText = "baz";
foo.AppendChild(bar);
root.AppendChild(foo);
doc.AppendChild(root);

So when you write markup you can put one xmlns="someURI" attribute on
the root element and it applies to the element itself and all descendant
elements. However if you create elements with the API then you need to
pass in the namespace URI each time you create an element.

You get
<Grant xmlns="">
as your code with
doc.CreateElement("Grant")
creates an element named "Grant" in _no namespace_ while you want an
element named "Grant" in a certain namespace, the namespace of the
parent you later insert the element in. To achieve that you need to pass
in the namespace to the CreateElement method. That does not create a
namespace declaration attribute, the serializer code is smart enough to
put that only once first element that needs that.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 23 '08 #11
Okay, I think I get it now. I changed my code to the following and it is now
giving me what I need.

XmlDocument doc = new XmlDocument();
doc.LoadXml(SiteUtility.GetFileText("~/_dev/acl.xml"));

const string amazonNsUri = "http://s3.amazonaws.com/doc/2006-03-01/";
XmlNamespaceManager xmlNM = new XmlNamespaceManager(doc.NameTable);
xmlNM.AddNamespace("def", amazonNsUri);

XmlNode acl =
doc.SelectSingleNode("/def:AccessControlPolicy/def:AccessControlList", xmlNM);

XmlNode grant = doc.CreateNode(XmlNodeType.Element, "Grant",
amazonNsUri);
XmlElement grantee = doc.CreateElement("Grantee", amazonNsUri);

XmlNode uri = doc.CreateNode(XmlNodeType.Element, "URI", amazonNsUri);
uri.InnerText = "http://acs.amazonaws.com/groups/global/AllUsers";

XmlNode permission = doc.CreateNode(XmlNodeType.Element,
"Permission", amazonNsUri);
permission.InnerText = "READ";

const string xmlns = "http://www.w3.org/2000/xmlns/";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";

XmlAttribute xsiNs = doc.CreateAttribute("xmlns", "xsi", xmlns);
xsiNs.Value = xsi;
grantee.SetAttributeNode(xsiNs);

XmlAttribute xsiType = doc.CreateAttribute("xsi", "type", xsi);
xsiType.Value = "Group";
grantee.SetAttributeNode(xsiType);

grant.AppendChild(grantee);
grant.AppendChild(permission);
grantee.AppendChild(uri);
acl.AppendChild(grant);

doc.Save("c:\\temp\\test.xml");


"Martin Honnen" wrote:
M1iS wrote:
But what if I don't want a namespace attribute at all on the grant node? I
want a node like this:

<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Group">
<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
</Grantee>
<Permission>READ</Permission>
</Grant>

I did not in any way suggest to put a namespace declaration attribute on
the Grant element, I did only suggest to pass in the namespace URI as a
parameter to the CreateElement method.

If you have markup of the form
<root xmlns="http://example.com/ns1"><foo><bar>baz</bar></foo></root>
then the namespace applies to all elements (e.g. the 'root' element, the
'foo' element, and the 'bar' element).

However if you want to create that structure with the DOM API then you
need to create each element in the namespace e.g.
const string ns = "http://example.com/ns1";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("root", ns);
XmlElement foo = doc.CreateElement("foo", ns);
XmlElement bar = doc.CreateElement("bar", ns);
bar.InnerText = "baz";
foo.AppendChild(bar);
root.AppendChild(foo);
doc.AppendChild(root);

So when you write markup you can put one xmlns="someURI" attribute on
the root element and it applies to the element itself and all descendant
elements. However if you create elements with the API then you need to
pass in the namespace URI each time you create an element.

You get
<Grant xmlns="">
as your code with
doc.CreateElement("Grant")
creates an element named "Grant" in _no namespace_ while you want an
element named "Grant" in a certain namespace, the namespace of the
parent you later insert the element in. To achieve that you need to pass
in the namespace to the CreateElement method. That does not create a
namespace declaration attribute, the serializer code is smart enough to
put that only once first element that needs that.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 23 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" );...
4
by: Victor Hadianto | last post by:
Hi, If I have an XmlDocument DOM how do I insert <?mso-application progid="ProgId.Here"?> programmatically? -- Victor Hadianto http://www.synop.com/Products/SauceReader/
2
by: Keith M | last post by:
Hi, I have found a very useful piece of code in the msdn which shows how to load an xml config file, search for a particular value and replace it. Could someone possibly convert it to C# from...
9
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
8
by: Ian Collins | last post by:
Is there a way to do this in IE? In Firefox, one can simply append a node from an XML return to the current document. IE whinges about 'No such interface supported' -- Ian Collins.
2
by: RichardHatcher.com | last post by:
I am writing C code using xpath to extract some data from a XML document. The file uses namespaces, but the name spaces are all listed in the root node of the file as attributes. I want to...
1
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
2
by: rds80 | last post by:
In the xml document below, I would like to retrieve the distinct attributes for the element '<Bal>'. However, I haven't had any success. Here is what I have so far: <TRANS> <TRAN...
12
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.