473,511 Members | 14,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding attribute to a xml element

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
Jun 27 '08 #1
6 10120
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
Jun 27 '08 #2
(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();
Jun 27 '08 #3
On May 19, 2:36*am, Marc Gravell <marc.grav...@gmail.comwrote:
(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.
Jun 27 '08 #4
On May 19, 8:21*am, CSharper <cshar...@gmx.comwrote:
On May 19, 2:36*am, Marc Gravell <marc.grav...@gmail.comwrote:
(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.
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.
Jun 27 '08 #5
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
Jun 27 '08 #6
On May 19, 2:08*pm, Marc Gravell <marc.grav...@gmail.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(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.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1622
by: Carlo | last post by:
I was wondering if someone could help me with this or point me to a good resource. I'm trying to add some attributes to docbook to be used later in effectivity. It was recommended I change the...
2
1025
by: David | last post by:
Hello. How can I add some attribute to "body" of the form from the code? P.S. I want to add "onLoad" event from code to execute some JavaScript function. Thank you.
7
3301
by: Joseph Scoccimaro | last post by:
Currently I am trying to use JavaScript within greasemonkey to dynamically put a menu at the top of each page. I am running in to trouble when I try to append a node representing a script tag to...
2
5925
by: darrel | last post by:
I am using a javascript WYSIWYG text editor for our CMS. To grab the proper content we need to add a javacsript call to the form: Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();") ...
3
1550
by: astruyk | last post by:
Is there a way to add an Attribute to an existing type? Like say I wanted to add a to the List<inttype, how would I go about doing that? Also is there any way to attach Attributes to types at...
4
1783
by: Sheri Orloff | last post by:
After using a 3-column template from Ben Meadowcroft which I like the looks of, I need to add an element but don't know how to do it. On the right side of the page...
1
18967
by: =?Utf-8?B?TmFiZWVsIE1vZWVu?= | last post by:
Hi, I want to add the xsi:nil="true' attribute to an element in XML. I am using XmlNode.Attributes.Append() but the generated output results in the attribute ignoring the "xsi:" prefix. How do...
1
2113
by: jambalapamba | last post by:
Hi I am walking xml using c# and i want add attribute (style="text-decoration: line-through;" ) to the parent node of text node if the value meets some condition. e.g if this...
0
1469
by: vamsioracle | last post by:
Hi all I have done a setup for a new absence type. Now my client wants to add balance to the existing leave balance. So i created an element with the following details Element Name: CTO leave...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.