473,668 Members | 2,386 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.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
Jun 27 '08 #1
6 10177
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
Jun 27 '08 #2
(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();
Jun 27 '08 #3
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.
Jun 27 '08 #4
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.
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(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
Jun 27 '08 #6
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.
Jun 27 '08 #7

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

Similar topics

1
1636
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 dbpoolx.mod: <!ENTITY % local.common.attrib ""> to <!ENTITY % local.common.attrib
2
1030
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
3313
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 the header section. It ends up not adding anything to the document. I am currently getting no errors from the javascript panel in firefox. Here is the code I am using: HeadTag = document.getElementsByTagName("head"); var scriptElement =...
2
5941
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();") That works fine. However, we also have some dropDownLists that trigger a postback. If we change these, I still need to update the text editor, so I added attributes to them:
3
1556
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 runtime?
4
1792
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 http://www.wicksworks.com/miabella/ ] is a box. How can I add an identical element just below it? Best wishes, Sheri Orloff Fun Home Based Candle Business For Under $50
1
19026
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 I work around this problem?.
1
2118
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 element <div >include</div> as include doesn't contain "read" i want <div style="text-decoration: line-through;">include</div> Here is wht i am doing XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(xmldoc);
0
1476
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 adjustment Category: Information Input Values: Days and Date. Now I added this element to the net calculation rules in the accrual plan.
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
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 we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.