adding attribute to a xml element

C

CSharper

I need to set an atribute to an xml element, I know how to do it with
XElement.SetAttributeValue method. But What I am trying to do is, I
need to add a attribute with a namespace like the following
test:name="tester"
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
 
M

Marc Gravell

Something like:

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

Marc
 
M

Marc Gravell

(example with a separate root element)

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

CSharper

(example with a separate root element)

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

Excellent, Thanks.
 
C

CSharper

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.
 
M

Marc Gravell

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(XNamespace.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
 
C

CSharper

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(XNamespace.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top