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