Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XML General > .NET and XML

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-20-2008, 11:40 PM
M1iS
 
Posts: n/a

Default 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/LogDeliver y</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);
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-20-2008, 11:40 PM
Navid
 
Posts: n/a

Default Re: Adding attributes to a node

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/LogDeliver y</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);
Reply With Quote
  #3 (permalink)  
Old 10-20-2008, 11:40 PM
Navid
 
Posts: n/a

Default Re: Adding attributes to a node

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/LogDeliver y</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()
Reply With Quote
  #4 (permalink)  
Old 10-21-2008, 12:19 AM
M1iS
 
Posts: n/a

Default 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:

> 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/LogDeliver y</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()
>

Reply With Quote
  #5 (permalink)  
Old 10-21-2008, 12:20 AM
Navid
 
Posts: n/a

Default Re: Adding attributes to a node

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/LogDeliver y</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://w ww.w3.org/2001/XMLSchema-instance");
attribute.Value = "Group";
grantee.Attributes.Append(attribute);

that should work.
Reply With Quote
  #6 (permalink)  
Old 10-21-2008, 12:54 AM
M1iS
 
Posts: n/a

Default Re: Adding attributes to a node

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/LogDeliver y</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://w ww.w3.org/2001/XMLSchema-instance");
> attribute.Value = "Group";
> grantee.Attributes.Append(attribute);
>
> that should work.
>

Reply With Quote
  #7 (permalink)  
Old 10-21-2008, 12:52 PM
Martin Honnen
 
Posts: n/a

Default Re: Adding attributes to a node

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/
Reply With Quote
  #8 (permalink)  
Old 10-22-2008, 07:20 PM
M1iS
 
Posts: n/a

Default 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/AllUse rs</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/AllUse rs</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/
>

Reply With Quote
  #9 (permalink)  
Old 10-23-2008, 01:40 PM
Martin Honnen
 
Posts: n/a

Default Re: Adding attributes to a node

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/AllUse rs</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/
Reply With Quote
  #10 (permalink)  
Old 10-23-2008, 06:11 PM
M1iS
 
Posts: n/a

Default 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.xm l"));

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/AllUse rs";

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/AllUse rs</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/
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 01:19 PM.
Style Developed by Epic Designz