473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to change attributes in an xml-file

Hi,

I wrote a small c# programm, which generates an xml-string by serializing a
class and sends it to a server. Now I want to change some values in that
xml-string.
The only way for me to do that might be, to deserialize it, change the
particular attribute and serialize it again, to finally save it on my
harddisk.
But I believe, that there is a more convenient way to do this, since it is
also possible to append an element...

Can anybody help me in this matter? What about the class XmlAttribute?
I appreciate for you help.

Cheers
Philipp
Jan 31 '06 #1
2 10047


Philipp wrote:

I wrote a small c# programm, which generates an xml-string by serializing a
class and sends it to a server. Now I want to change some values in that
xml-string.
The only way for me to do that might be, to deserialize it, change the
particular attribute and serialize it again, to finally save it on my
harddisk. But I believe, that there is a more convenient way to do this, since it is
also possible to append an element...


If you have a string with an XML document then you need to deserialize
to manipulate that. You can do that as you already seem to do, by
deserializing to an instance object of your class and manipulating the
properties of the object. Or you could use XmlDocument, the DOM
implementation in .NET to do that e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(yourString);
// now find the element whose attribute you want to change e.g.
XmlElement element =
xmlDocument.SelectSingleNode("SomeXPathExpression" ) as XmlElement;
if (element != null) {
element.SetAttribute("attribute-name", "new attribute value");
}
// how serialize to string e.g. using
xmlDocument.OuterXml
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 31 '06 #2
OK, I´ve solved the problem. It was just to easy to see :-)

xdoc.SelectSingleNode("root/node1/node2").InnerText = "123456";

Bye,
Philipp

"Philipp" <fi***@suedmeyer.net> schrieb im Newsbeitrag
news:44***********@news.dfncis.de...
Hi,

I wrote a small c# programm, which generates an xml-string by serializing
a class and sends it to a server. Now I want to change some values in that
xml-string.
The only way for me to do that might be, to deserialize it, change the
particular attribute and serialize it again, to finally save it on my
harddisk.
But I believe, that there is a more convenient way to do this, since it is
also possible to append an element...

Can anybody help me in this matter? What about the class XmlAttribute?
I appreciate for you help.

Cheers
Philipp

Jan 31 '06 #3

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

Similar topics

7
by: Info 3000 | last post by:
Hello, I'm beginner in XML. I have just a little question : I understand that I can write : <Book> <Title> A nice day </Title> <Author> James Nicepen </Author> </Book>
3
by: zerdust | last post by:
how can I change the name of XML node? this is sample <?xml version="1.0"?> <adresboek> <adres> <naam>Mark van Renswoude</naam> <straat>ddden 24</straat> <plaats>Scddd</plaats>...
2
by: Jim Lewis | last post by:
After everything I have read and some of my own testing I am convinced that XPathDocument are more efficient if you are only using XML for read only and not modifying the XML. However, I have been...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
2
by: Shailendra Batham | last post by:
Hello Gurus, I want to put some restrictions on my attribute tag in my XML Schema, anyone out there have any idea how to do that. here is my XML and the XML Schema <?xml version="1.0"...
3
by: Grant | last post by:
How would I change the value of an attribute in an XML node using C#? I have tried "findnode.Attributes.InnerText = newValue;" and "Attribute attri = (XmlAttribute)findnode.Attributes.Value;" ...
2
by: Rick | last post by:
I have an XML document that is generated from Infopath, I need to change the value of a namespace that is defined in a node in the form: <xsf:xDocumentClass "xmlns:my=valuehere">. when i navigate...
0
by: beanweed | last post by:
BACKGROUND ---------- I have an ASP.NET application having two panels. In one panel, an XML document, transformed using xsl, is displayed. In the other panel are some controls that allow a user...
4
by: ChillyRoll | last post by:
Hello guys, I am looking for a parser in PHP that can return all the attributes of XML entities. I know how to read the XML Entities, but I have got a problem with reading attributes. So I will...
2
by: Mikus Sleiners | last post by:
I want to create xml document that looks like this: <?xml version="1.0" encoding="utf-16"?> <Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.