Connecting Tech Pros Worldwide Forums | Help | Site Map

Beginners Question...

Peter Treier
Guest
 
Posts: n/a
#1: Nov 11 '05
I got a XML file like this:

<?xml version="1.0" encoding="utf-16"?>
<LicenceControlFile>
<Licence1>2AB7-FA43-B05C-4C2A-8DBD</Licence1>
<MachineName />
<Licence2>2AB7-FA43-B05C-4C2A-8DBD</Licence2>
<MachineName />
<Licence3>2AB7-FA43-B05C-4C2A-8DBD</Licence3>
<MachineName />
</LicenceControlFile>

Now i need to set the Value of one of the <MachineName /> Elements !
How can i do this with .NET...

Thanks

Peter



Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 11 '05

re: Beginners Question...


Peter Treier wrote:
[color=blue]
> <?xml version="1.0" encoding="utf-16"?>
> <LicenceControlFile>
> <Licence1>2AB7-FA43-B05C-4C2A-8DBD</Licence1>
> <MachineName />
> <Licence2>2AB7-FA43-B05C-4C2A-8DBD</Licence2>
> <MachineName />
> <Licence3>2AB7-FA43-B05C-4C2A-8DBD</Licence3>
> <MachineName />
> </LicenceControlFile>
>
> Now i need to set the Value of one of the <MachineName /> Elements !
> How can i do this with .NET...[/color]

The simplest (but not necessarily the most effective) way is to load document
to XmlDocument, select appropriate MachineName element using SelectSingleNode
method, update it and save document back.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Peter Treier
Guest
 
Posts: n/a
#3: Nov 11 '05

re: Beginners Question...


Good evening Oleg

I've tried to do that, but i got one Error

<---Error

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll

Additional information: Cannot set a value on node type: Element.

--->End Error

I've selected the first Element with Xpath


loDoc is a XMLDocument

loDoc.SelectSingleNode("/LicenceControlFile/MachineName").Value =
sMachineName



Whats wrong ?

Regards from Switzerland

Peter

"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:eD8UFN6dDHA.568@TK2MSFTNGP11.phx.gbl...[color=blue]
> Peter Treier wrote:
>[color=green]
> > <?xml version="1.0" encoding="utf-16"?>
> > <LicenceControlFile>
> > <Licence1>2AB7-FA43-B05C-4C2A-8DBD</Licence1>
> > <MachineName />
> > <Licence2>2AB7-FA43-B05C-4C2A-8DBD</Licence2>
> > <MachineName />
> > <Licence3>2AB7-FA43-B05C-4C2A-8DBD</Licence3>
> > <MachineName />
> > </LicenceControlFile>
> >
> > Now i need to set the Value of one of the <MachineName /> Elements !
> > How can i do this with .NET...[/color]
>
> The simplest (but not necessarily the most effective) way is to load[/color]
document[color=blue]
> to XmlDocument, select appropriate MachineName element using[/color]
SelectSingleNode[color=blue]
> method, update it and save document back.
> --
> Oleg Tkachenko
> http://www.tkachenko.com/blog
> Multiconn Technologies, Israel
>[/color]


Oleg Tkachenko
Guest
 
Posts: n/a
#4: Nov 11 '05

re: Beginners Question...


Peter Treier wrote:

[color=blue]
> I've tried to do that, but i got one Error
>
> <---Error
>
> An unhandled exception of type 'System.InvalidOperationException' occurred
> in system.xml.dll
>
> Additional information: Cannot set a value on node type: Element.
>
> --->End Error
>
> I've selected the first Element with Xpath
>
>
> loDoc is a XMLDocument
>
> loDoc.SelectSingleNode("/LicenceControlFile/MachineName").Value =
> sMachineName
>
>
>
> Whats wrong ?[/color]

Forgot to mention, you have to create text node. In DOM elements have no
value. Text is stored in text nodes' value. So you have to create text node,
set its value and append it as a child to the element.
Something like
XmlNode elem = loDoc.SelectSingleNode("/LicenceControlFile/MachineName");
XmlText textNode = loDoc.CreateTextNode(sMachineName);
elem.AppendChild(textNode);

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Closed Thread