Adding attributes to a node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TTFpUw==?=

    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.c om/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.Att ribute, "xsi:type", "");
    attribute.Value = "Group";
    grantee.Attribu tes.SetNamedIte m(attribute);

    attribute = doc.CreateNode( XmlNodeType.Att ribute, "xmlns:xsi" , "");
    attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
    grantee.Attribu tes.SetNamedIte m(attribute);
  • Navid

    #2
    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.c om/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.Att ribute, "xsi:type", "");
    attribute.Value = "Group";
    grantee.Attribu tes.SetNamedIte m(attribute);
    >
    attribute = doc.CreateNode( XmlNodeType.Att ribute, "xmlns:xsi" , "");
    attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
    grantee.Attribu tes.SetNamedIte m(attribute);
    instead of doing grantee.Attribu tes.SetNamedIte m(attribute);
    do grantee.Attribu tes.append(attr ibute);

    Comment

    • Navid

      #3
      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.c om/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.Att ribute, "xsi:type", "");
      attribute.Value = "Group";
      grantee.Attribu tes.SetNamedIte m(attribute);
      >
      attribute = doc.CreateNode( XmlNodeType.Att ribute, "xmlns:xsi" , "");
      attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
      grantee.Attribu tes.SetNamedIte m(attribute);
      Sorry also instead of doc.CreateNode, it should be doc.CreateAttri bute()

      Comment

      • =?Utf-8?B?TTFpUw==?=

        #4
        Re: Adding attributes to a node

        Okay I tried the following:

        XmlAttribute attribute;

        attribute = doc.CreateAttri bute("xmlns:xsi ");
        attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
        grantee.Attribu tes.Append(attr ibute);

        attribute = doc.CreateAttri bute("xsi:type" );
        attribute.Value = "Group";
        grantee.Attribu tes.Append(attr ibute);

        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.c om/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.Att ribute, "xsi:type", "");
        attribute.Value = "Group";
        grantee.Attribu tes.SetNamedIte m(attribute);

        attribute = doc.CreateNode( XmlNodeType.Att ribute, "xmlns:xsi" , "");
        attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
        grantee.Attribu tes.SetNamedIte m(attribute);
        Sorry also instead of doc.CreateNode, it should be doc.CreateAttri bute()
        >

        Comment

        • Navid

          #5
          Re: Adding attributes to a node

          M1iS wrote:
          Okay I tried the following:
          >
          XmlAttribute attribute;
          >
          attribute = doc.CreateAttri bute("xmlns:xsi ");
          attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
          grantee.Attribu tes.Append(attr ibute);
          >
          attribute = doc.CreateAttri bute("xsi:type" );
          attribute.Value = "Group";
          grantee.Attribu tes.Append(attr ibute);
          >
          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="Gro up">
          >> <URI>http://acs.amazonaws.c om/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.Att ribute, "xsi:type", "");
          >> attribute.Value = "Group";
          >> grantee.Attribu tes.SetNamedIte m(attribute);
          >>>
          >> attribute = doc.CreateNode( XmlNodeType.Att ribute, "xmlns:xsi" , "");
          >> attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
          >> grantee.Attribu tes.SetNamedIte m(attribute);
          >Sorry also instead of doc.CreateNode, it should be doc.CreateAttri bute()
          >>
          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.C reateAttribute( "xsi:type","htt p://www.w3.org/2001/XMLSchema-instance");
          attribute.Value = "Group";
          grantee.Attribu tes.Append(attr ibute);

          that should work.

          Comment

          • =?Utf-8?B?TTFpUw==?=

            #6
            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.CreateAttri bute("xmlns:xsi ");
            attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
            grantee.Attribu tes.Append(attr ibute);

            attribute = doc.CreateAttri bute("xsi:type" );
            attribute.Value = "Group";
            grantee.Attribu tes.Append(attr ibute);

            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="Grou p">
            > <URI>http://acs.amazonaws.c om/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.Att ribute, "xsi:type", "");
            > attribute.Value = "Group";
            > grantee.Attribu tes.SetNamedIte m(attribute);
            >>
            > attribute = doc.CreateNode( XmlNodeType.Att ribute, "xmlns:xsi" , "");
            > attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
            > grantee.Attribu tes.SetNamedIte m(attribute);
            Sorry also instead of doc.CreateNode, it should be doc.CreateAttri bute()
            >
            >
            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.C reateAttribute( "xsi:type","htt p://www.w3.org/2001/XMLSchema-instance");
            attribute.Value = "Group";
            grantee.Attribu tes.Append(attr ibute);
            >
            that should work.
            >

            Comment

            • Martin Honnen

              #7
              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.CreateEleme nt("Grantee");

              XmlAttribute xsiNs = doc.CreateAttri bute("xmlns", "xsi", xmlns);
              xsiNs.Value = xsi;
              grantee.SetAttr ibuteNode(xsiNs );

              XmlAttribute xsiType = doc.CreateAttri bute("xsi", "type", xsi);
              xsiType.Value = "Group";
              grantee.SetAttr ibuteNode(xsiTy pe);

              doc.AppendChild (grantee);

              doc.Save(Consol e.Out);


              --

              Martin Honnen --- MVP XML

              Comment

              • =?Utf-8?B?TTFpUw==?=

                #8
                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.c om/groups/global/AllUsers</URI>
                </Grantee>
                <Permission>REA D</Permission>
                </Grant>

                Here is my entire block of code:

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(Sit eUtility.GetFil eText("~/_dev/acl.xml"));

                XmlNamespaceMan ager xmlNM = new XmlNamespaceMan ager(doc.NameTa ble);
                xmlNM.AddNamesp ace("def", @"http://s3.amazonaws.co m/doc/2006-03-01/");

                XmlNode acl =
                doc.SelectSingl eNode("/def:AccessContr olPolicy/def:AccessContr olList", xmlNM);

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

                XmlNode uri = doc.CreateNode( XmlNodeType.Ele ment, "URI", "");
                uri.InnerText = "http://acs.amazonaws.c om/groups/global/AllUsers";

                XmlNode permission = doc.CreateNode( XmlNodeType.Ele ment,
                "Permission ", "");
                permission.Inne rText = "READ";

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

                XmlAttribute xsiNs = doc.CreateAttri bute("xmlns", "xsi", xmlns);
                xsiNs.Value = xsi;
                grantee.SetAttr ibuteNode(xsiNs );

                XmlAttribute xsiType = doc.CreateAttri bute("xsi", "type", xsi);
                xsiType.Value = "Group";
                grantee.SetAttr ibuteNode(xsiTy pe);

                grant.AppendChi ld(grantee);
                grant.AppendChi ld(permission);
                grantee.AppendC hild(uri);
                acl.AppendChild (grant);

                doc.Save("c:\\t emp\\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.CreateEleme nt("Grantee");
                >
                XmlAttribute xsiNs = doc.CreateAttri bute("xmlns", "xsi", xmlns);
                xsiNs.Value = xsi;
                grantee.SetAttr ibuteNode(xsiNs );
                >
                XmlAttribute xsiType = doc.CreateAttri bute("xsi", "type", xsi);
                xsiType.Value = "Group";
                grantee.SetAttr ibuteNode(xsiTy pe);
                >
                doc.AppendChild (grantee);
                >
                doc.Save(Consol e.Out);
                >
                >
                --
                >
                Martin Honnen --- MVP XML

                >

                Comment

                • Martin Honnen

                  #9
                  Re: Adding attributes to a node

                  M1iS wrote:
                  <Grant xmlns="">
                  XmlElement grant = doc.CreateEleme nt("Grant");
                  XmlElement grantee = doc.CreateEleme nt("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.CreateEleme nt("Grant", someNs);
                  XmlElement grantee = doc.CreateEleme nt("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.NamespaceUR I;
                  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

                  Comment

                  • =?Utf-8?B?TTFpUw==?=

                    #10
                    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.c om/groups/global/AllUsers</URI>
                    </Grantee>
                    <Permission>REA D</Permission>
                    </Grant>

                    Rather than this:

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




                    "Martin Honnen" wrote:
                    M1iS wrote:
                    >
                    <Grant xmlns="">
                    >
                    XmlElement grant = doc.CreateEleme nt("Grant");
                    XmlElement grantee = doc.CreateEleme nt("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.CreateEleme nt("Grant", someNs);
                    XmlElement grantee = doc.CreateEleme nt("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.NamespaceUR I;
                    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

                    >

                    Comment

                    • Martin Honnen

                      #11
                      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.c om/groups/global/AllUsers</URI>
                      </Grantee>
                      <Permission>REA D</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.CreateEleme nt("root", ns);
                      XmlElement foo = doc.CreateEleme nt("foo", ns);
                      XmlElement bar = doc.CreateEleme nt("bar", ns);
                      bar.InnerText = "baz";
                      foo.AppendChild (bar);
                      root.AppendChil d(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.CreateEleme nt("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

                      Comment

                      • =?Utf-8?B?TTFpUw==?=

                        #12
                        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(Sit eUtility.GetFil eText("~/_dev/acl.xml"));

                        const string amazonNsUri = "http://s3.amazonaws.co m/doc/2006-03-01/";
                        XmlNamespaceMan ager xmlNM = new XmlNamespaceMan ager(doc.NameTa ble);
                        xmlNM.AddNamesp ace("def", amazonNsUri);

                        XmlNode acl =
                        doc.SelectSingl eNode("/def:AccessContr olPolicy/def:AccessContr olList", xmlNM);

                        XmlNode grant = doc.CreateNode( XmlNodeType.Ele ment, "Grant",
                        amazonNsUri);
                        XmlElement grantee = doc.CreateEleme nt("Grantee", amazonNsUri);

                        XmlNode uri = doc.CreateNode( XmlNodeType.Ele ment, "URI", amazonNsUri);
                        uri.InnerText = "http://acs.amazonaws.c om/groups/global/AllUsers";

                        XmlNode permission = doc.CreateNode( XmlNodeType.Ele ment,
                        "Permission ", amazonNsUri);
                        permission.Inne rText = "READ";

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

                        XmlAttribute xsiNs = doc.CreateAttri bute("xmlns", "xsi", xmlns);
                        xsiNs.Value = xsi;
                        grantee.SetAttr ibuteNode(xsiNs );

                        XmlAttribute xsiType = doc.CreateAttri bute("xsi", "type", xsi);
                        xsiType.Value = "Group";
                        grantee.SetAttr ibuteNode(xsiTy pe);

                        grant.AppendChi ld(grantee);
                        grant.AppendChi ld(permission);
                        grantee.AppendC hild(uri);
                        acl.AppendChild (grant);

                        doc.Save("c:\\t emp\\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.c om/groups/global/AllUsers</URI>
                        </Grantee>
                        <Permission>REA D</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.CreateEleme nt("root", ns);
                        XmlElement foo = doc.CreateEleme nt("foo", ns);
                        XmlElement bar = doc.CreateEleme nt("bar", ns);
                        bar.InnerText = "baz";
                        foo.AppendChild (bar);
                        root.AppendChil d(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.CreateEleme nt("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

                        >

                        Comment

                        Working...