adding attribute to a xml element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    adding attribute to a xml element

    I need to set an atribute to an xml element, I know how to do it with
    XElement.SetAtt ributeValue method. But What I am trying to do is, I
    need to add a attribute with a namespace like the following
    test:name="test er"
    When I try to add, it fails with : not a valid character in attribute.
    So my question is
    How do I go about adding the new namespace to XDocument and also the
    the attribute so that set attribute value doesn't fail.

    Thanks you very much
  • Marc Gravell

    #2
    Re: adding attribute to a xml element

    Something like:

    XNamespace ns = "urn:my-namespace";
    XElement el = new XElement("Foo",
    new XAttribute(XNam espace.Xmlns + "test", ns),
    new XAttribute(ns + "name", "tester"));
    string xml = el.ToString();

    Marc

    Comment

    • Marc Gravell

      #3
      Re: adding attribute to a xml element

      (example with a separate root element)

      XNamespace ns = "urn:my-namespace";
      XDocument doc = new XDocument(
      new XElement("Foo",
      new XAttribute(XNam espace.Xmlns + "test", ns),
      new XElement("Bar",
      new XAttribute(ns + "name", "tester"))) );
      string xml = doc.ToString();

      Comment

      • CSharper

        #4
        Re: adding attribute to a xml element

        On May 19, 2:36 am, Marc Gravell <marc.grav...@g mail.comwrote:
        (example with a separate root element)
        >
             XNamespace ns = "urn:my-namespace";
             XDocument doc = new XDocument(
                 new XElement("Foo",
                     new XAttribute(XNam espace.Xmlns + "test", ns),
                     new XElement("Bar",
                         new XAttribute(ns + "name", "tester"))) );
             string xml = doc.ToString();
        Excellent, Thanks.

        Comment

        • CSharper

          #5
          Re: adding attribute to a xml element

          On May 19, 8:21 am, CSharper <cshar...@gmx.c omwrote:
          On May 19, 2:36 am, Marc Gravell <marc.grav...@g mail.comwrote:
          >
          (example with a separate root element)
          >
               XNamespace ns = "urn:my-namespace";
               XDocument doc = new XDocument(
                   new XElement("Foo",
                       new XAttribute(XNam espace.Xmlns + "test", ns),
                       new XElement("Bar",
                           new XAttribute(ns + "name", "tester"))) );
               string xml = doc.ToString();
          >
          Excellent, Thanks.
          One quick question, right now with this it works, but by default the
          program is creating a namepsace of 'p7', is there a way, I can force
          the p7 to a different name say csharp?

          Thanks.

          Comment

          • Marc Gravell

            #6
            Re: adding attribute to a xml element

            One quick question, right now with this it works, but by default the
            program is creating a namepsace of 'p7', is there a way, I can force
            the p7 to a different name say csharp?
            It is creating an *alias* of p7 because you haven't added the xmlns
            attribute to the root. Note that my example doesn't generate any p7,
            p2, etc - unless I drop the line:
            new XAttribute(XNam espace.Xmlns + "test", ns),

            Include a similar line, aliasing your namespace as whatever (where
            I've aliased as "test") and you should be sorted. If you have
            problems, can you post a short but complete example? (since the code
            works "as posted", we'd need to look at what is different...)

            Marc

            Comment

            • CSharper

              #7
              Re: adding attribute to a xml element

              On May 19, 2:08 pm, Marc Gravell <marc.grav...@g mail.comwrote:
              One quick question, right now with this it works, but by default the
              program is creating a namepsace of 'p7', is there a way, I can force
              the p7 to a different name say csharp?
              >
              It is creating an *alias* of p7 because you haven't added the xmlns
              attribute to the root. Note that my example doesn't generate any p7,
              p2, etc - unless I drop the line:
                  new XAttribute(XNam espace.Xmlns + "test", ns),
              >
              Include a similar line, aliasing your namespace as whatever (where
              I've aliased as "test") and you should be sorted. If you have
              problems, can you post a short but complete example? (since the code
              works "as posted", we'd need to look at what is different...)
              >
              Marc
              Thanks, I was missing the alias.

              Comment

              Working...