Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding attributes to a node

=?Utf-8?B?TTFpUw==?=
Guest
 
Posts: n/a
#1: Oct 20 '08
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);

Navid
Guest
 
Posts: n/a
#2: Oct 20 '08

re: Adding attributes to a node


M1iS wrote:
Quote:
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);
Navid
Guest
 
Posts: n/a
#3: Oct 20 '08

re: Adding attributes to a node


M1iS wrote:
Quote:
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()
=?Utf-8?B?TTFpUw==?=
Guest
 
Posts: n/a
#4: Oct 21 '08

re: Adding attributes to a node


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:
Quote:
M1iS wrote:
Quote:
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()
>
Navid
Guest
 
Posts: n/a
#5: Oct 21 '08

re: Adding attributes to a node


M1iS wrote:
Quote:
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:
>
Quote:
>M1iS wrote:
Quote:
>>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.
=?Utf-8?B?TTFpUw==?=
Guest
 
Posts: n/a
#6: Oct 21 '08

re: Adding attributes to a node


Thanks so much, that did the trick.


"Navid" wrote:
Quote:
M1iS wrote:
Quote:
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:
Quote:
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.
>
Martin Honnen
Guest
 
Posts: n/a
#7: Oct 21 '08

re: Adding attributes to a node


M1iS wrote:
Quote:
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/
=?Utf-8?B?TTFpUw==?=
Guest
 
Posts: n/a
#8: Oct 22 '08

re: Adding attributes to a node


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:
Quote:
M1iS wrote:
Quote:
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/
>
Martin Honnen
Guest
 
Posts: n/a
#9: Oct 22 '08

re: Adding attributes to a node


M1iS wrote:
Quote:
<Grant xmlns="">
Quote:
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/
=?Utf-8?B?TTFpUw==?=
Guest
 
Posts: n/a
#10: Oct 22 '08

re: Adding attributes to a node


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:
Quote:
M1iS wrote:
>
Quote:
<Grant xmlns="">
>
Quote:
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/
>
Martin Honnen
Guest
 
Posts: n/a
#11: Oct 23 '08

re: Adding attributes to a node


M1iS wrote:
Quote:
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/
=?Utf-8?B?TTFpUw==?=
Guest
 
Posts: n/a
#12: Oct 23 '08

re: Adding attributes to a node


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:
Quote:
M1iS wrote:
Quote:
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/
>
Closed Thread