Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

adding attribute to a xml element

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CSharper - 19 May 2008 04:43 GMT
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
Marc Gravell - 19 May 2008 08:20 GMT
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
Marc Gravell - 19 May 2008 08:36 GMT
(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();
CSharper - 19 May 2008 14:21 GMT
> (example with a separate root element)
>
[quoted text clipped - 5 lines]
>                  new XAttribute(ns + "name", "tester"))));
>      string xml = doc.ToString();

Excellent, Thanks.
CSharper - 19 May 2008 15:21 GMT
> > (example with a separate root element)
>
[quoted text clipped - 7 lines]
>
> 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.
Marc Gravell - 19 May 2008 20:08 GMT
> 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
CSharper - 20 May 2008 15:22 GMT
> > 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
[quoted text clipped - 11 lines]
>
> Marc

Thanks, I was missing the alias.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.