Writing to a text file formatted as XML

Hi!
I have a text file that looks like this:
<configuration>
  <appSettings>

    <add key="db_server"	value="someserver" />
    <add key="db_database"	value="somedatabase" />

  </appSettings>
</configuration> 

Now, I want to check if value for db_server is empty and if it is, I want to 
write the value. I am trying to use XmlTextWriter and XmlTextReader but being 
new to XML processing I don't know exactly how to do it.

Can anyone help please?
Regards,
Kumar
0
Rajkumar (10)
8/18/2004 7:55:02 PM
dotnet.xml 7266 articles. 0 followers. Follow

3 Replies
595 Views

Similar Articles

[PageSpeed] 11

The easiest way would be to load the document in XmlDocument and edit the
value of the node. (The following code uses XPath to find the empty value
attributes)

XmlDocument doc = new XmlDocument();
 doc.Load("old.xml");
 XmlNodeList list =
doc.SelectNodes("/configuration/appSettings/add[@value='']/@value");
 foreach(XmlAttribute attr in list)
 {
  attr.Value = "test";
 }
 doc.Save("new.xml");


"Raj Kumar" <RajKumar@discussions.microsoft.com> wrote in message
news:386C8BBF-81F5-47C4-94DA-2ECEE109AE1A@microsoft.com...
> Hi!
> I have a text file that looks like this:
> <configuration>
>   <appSettings>
>
>     <add key="db_server" value="someserver" />
>     <add key="db_database" value="somedatabase" />
>
>   </appSettings>
> </configuration>
>
> Now, I want to check if value for db_server is empty and if it is, I want
to
> write the value. I am trying to use XmlTextWriter and XmlTextReader but
being
> new to XML processing I don't know exactly how to do it.
>
> Can anyone help please?
> Regards,
> Kumar


0
asadj (2)
8/19/2004 12:33:05 AM
Hi! Thank you for responding :-)
I am working with C# in .NET compact framework and it doesnot seem to 
support doc.SelectNodes. Instead we can use GetElementsByTagName but then I 
don't know what should go in there. I tried:

node_list = xmlDoc.GetElementsByTagName("/configuration/appSettings/add 
[@key='db_server'] [@value='']");
			
foreach(XmlAttribute attr in node_list)
{
	attr.Value = server_name;
}
xmlDoc.Save(file_path);

This did not give out anything. Any other ideas?

Thanks and regards,
Kumar

"Asad Jawahar" wrote:

> The easiest way would be to load the document in XmlDocument and edit the
> value of the node. (The following code uses XPath to find the empty value
> attributes)
> 
> XmlDocument doc = new XmlDocument();
>  doc.Load("old.xml");
>  XmlNodeList list =
> doc.SelectNodes("/configuration/appSettings/add[@value='']/@value");
>  foreach(XmlAttribute attr in list)
>  {
>   attr.Value = "test";
>  }
>  doc.Save("new.xml");
> 
> 
> "Raj Kumar" <RajKumar@discussions.microsoft.com> wrote in message
> news:386C8BBF-81F5-47C4-94DA-2ECEE109AE1A@microsoft.com...
> > Hi!
> > I have a text file that looks like this:
> > <configuration>
> >   <appSettings>
> >
> >     <add key="db_server" value="someserver" />
> >     <add key="db_database" value="somedatabase" />
> >
> >   </appSettings>
> > </configuration>
> >
> > Now, I want to check if value for db_server is empty and if it is, I want
> to
> > write the value. I am trying to use XmlTextWriter and XmlTextReader but
> being
> > new to XML processing I don't know exactly how to do it.
> >
> > Can anyone help please?
> > Regards,
> > Kumar
> 
> 
> 
0
Rajkumar (10)
8/19/2004 1:41:04 PM
In this case you cannot use XPath but you can write some additional code to
make it work using GetElementByTagName. First find all 'add' elements and
then get the attributes you need to modify:

 XmlDocument doc = new XmlDocument();
 doc.Load("a.xml");
 XmlNodeList list = doc.GetElementsByTagName("add");
 foreach(XmlElement elmt in list)
 {
  XmlAttribute attr = elmt.Attributes["key"];
  if(attr != null && attr.Value=="db_server")
  {
   elmt.Attributes["value"].Value = "test";
  }
 }
 doc.Save("b.xml");

The GetElementByTagName will return all 'key' elements in the file (as
opposed to only the ones under configuration/appSettings) so this solution
should work as long as there are no other elements in the xml with the same
name ('key') that serve a different purpose. In case there are then you can
use a differnt approach by manually walking the DOM tree.


"Raj Kumar" <RajKumar@discussions.microsoft.com> wrote in message
news:F58058A3-3D1E-4542-B0CF-B9F6731ECEAE@microsoft.com...
> Hi! Thank you for responding :-)
> I am working with C# in .NET compact framework and it doesnot seem to
> support doc.SelectNodes. Instead we can use GetElementsByTagName but then
I
> don't know what should go in there. I tried:
>
> node_list = xmlDoc.GetElementsByTagName("/configuration/appSettings/add
> [@key='db_server'] [@value='']");
>
> foreach(XmlAttribute attr in node_list)
> {
> attr.Value = server_name;
> }
> xmlDoc.Save(file_path);
>
> This did not give out anything. Any other ideas?
>
> Thanks and regards,
> Kumar
>
> "Asad Jawahar" wrote:
>
> > The easiest way would be to load the document in XmlDocument and edit
the
> > value of the node. (The following code uses XPath to find the empty
value
> > attributes)
> >
> > XmlDocument doc = new XmlDocument();
> >  doc.Load("old.xml");
> >  XmlNodeList list =
> > doc.SelectNodes("/configuration/appSettings/add[@value='']/@value");
> >  foreach(XmlAttribute attr in list)
> >  {
> >   attr.Value = "test";
> >  }
> >  doc.Save("new.xml");
> >
> >
> > "Raj Kumar" <RajKumar@discussions.microsoft.com> wrote in message
> > news:386C8BBF-81F5-47C4-94DA-2ECEE109AE1A@microsoft.com...
> > > Hi!
> > > I have a text file that looks like this:
> > > <configuration>
> > >   <appSettings>
> > >
> > >     <add key="db_server" value="someserver" />
> > >     <add key="db_database" value="somedatabase" />
> > >
> > >   </appSettings>
> > > </configuration>
> > >
> > > Now, I want to check if value for db_server is empty and if it is, I
want
> > to
> > > write the value. I am trying to use XmlTextWriter and XmlTextReader
but
> > being
> > > new to XML processing I don't know exactly how to do it.
> > >
> > > Can anyone help please?
> > > Regards,
> > > Kumar
> >
> >
> >


0
asadj (2)
8/19/2004 5:15:49 PM
Reply:

Similar Artilces:

removing a text box in a chart
I've inserted a text box into a chart but realise i no longer need it. How do you remove it? any help would be great. Oliver Hi, Have you tried selecting it and pressing the Delete key? Cheers Andy Oliver S wrote: > I've inserted a text box into a chart but realise i no longer need it. > > How do you remove it? > > any help would be great. > Oliver ...

Enlarge text in Inbox
Is there a way to increase font tesxt size in the INBOX mailbox before opening mail? How depends somewhat on your version of Outlook. View | Current View | Customize Current View | Other Settings in some versions. -- -Ben- Ben M. Schorr, MVP Roland Schorr & Tower http://www.rolandschorr.com http://www.officeforlawyers.com/outlook.htm Author: The Lawyer's Guide to Microsoft Outlook 2007: http://tinyurl.com/ol4law-amazon "bb indy" <bb indy@discussions.microsoft.com> wrote in message news:1F0F5A0F-0668-4728-8BC1-694FE6A7CB61@microsoft.com... >...

Date format #3
I have two sheets that includes the date m/d/y which is to be transferred to the second sheet. If a date isnt entered on the first sheet I want it show as blank on second sheet. Right now it is showing 1/0/00. how can I change this? Thanks Hi Wally try the following fomule on your second sheet =IF('sheet1'!A1="","",'sheet1'!A1) -- Regards Frank Kabel Frankfurt, Germany Wally wrote: > I have two sheets that includes the date m/d/y which is to be > transferred to the second sheet. If a date isnt entered on the first > sheet I want it show as...

Is there a way for Windows Service to Read/Write HKCU Reg key ?
Hi: I'm writing a Windows Service program which needs to read/write the HKEY_CURRENT_USER registry key for the current logged on user. I know that for a Windows service does not have concept of HKCU since it is always running in SYSTEM's context. But is there a way (trick?) for a service to access the HKCU registry key? Thanks In Advance ! Polaris Hello Try, from a user application (the one that is logged on, or the current user), to send the services the user's SID. Then access the HKCU through HKEY_USERS\users_sid\............ Hope that helps, Elias "Polaris&qu...

Macro Help- combining "CS" files
Below is the macro I have to go to a certain file and combine all spreadsheets. I did not write this macro myself. I just received it and modified it to work for my situation. When this maco is run it gets to the first file and says I cannot change a read only file and says I must unprotect the worksheet. This sheet is not protected but I really only want to copy the info on it anyway. Is there a way to modify this macro to copy the information. I could save all of the "CS" files as new files but that would defeat the purpose of automating this job Any help is greatly a...

Import data from DBF files
What's the best way to import a large dbf file into Access (records from a DBase file)? I have one dbf that has over 220,000 records in it. It gets to about 75% and says "The search key was not found in any record." Everything is patched to the max (Windows, Office, Jet, etc). This doesn't happen with any other dbf file, but they are all smaller. Also, oddly, I can copy and paste 50,000 records in a shot and paste them through a query, but I don't want to keep doing that for every large dbf I have. The Access tables have no problem holding the data, just transf...

pst files and VPN issues
I'm looking for suggestions on how to configure .pst files for remote users who access a network using VPN. Due to the large size of these files (often over a gigabyte), Outlook's performance is seriously affected and often freezes during the archive process. What are your company's polices for remote users accessing .pst files and is there a way short of saving them locally to access them without affecting Outlook's performance? If the .pst has to be stored and backed up locally, does anyone have security advice for protecting .pst files with sensitive material? Encrypted f...

"File not found" and "The form name is misspelled..." errors
I am still bulding a database. It contains no data, and contains no embedded macros. Yet, when I go to build code, I get these errors: "File not found" followed by "The form name * is misspelled or refers to a form the doesn't exist" The problem does not lie in the form that automatically opens when the database opens, and there are no embedded macros (I've read that these are the two main places an error such as this can occur). I did a search in the code box, and do find the two subforms listed even though neither contains any kind of code. I ca...

Write
I am trying to change the default margins. I am requested to use the Write mode to do this. What is Write, and how do I access it? Are you using voice recognition? Margins are simply changed in the Arrange Menu in Publisher. Are you in the right newsgroup? -- Mary Sauer MS MVP http://office.microsoft.com/ http://www.mvps.org/msauer/ news://msnews.microsoft.com "CBC" <anonymous@discussions.microsoft.com> wrote in message news:183bb01c44a40$107bd5d0$a501280a@phx.gbl... > I am trying to change the default margins. I am requested > to use the Write mode to do this. W...

Conditioned formatting
I have a conditional format based on this formula: ="OR(WEEKDAY(A1)=6;WEEKDAY(A1)=7)" How do I copy this conditinal format to other cells and have the cell reference change to the appropriate cell address. If I just copy the format, using the format painter, it does not update the cell reference! Jan I maybe should add, that I cant just coipy the cell, as the cells, that should use the conditional format already contains different formulas. The only thing they have in common is that they create a date. Jan "sgdgr" <jkr@spammerlyngby.nu> wrote in message news:%...

How to get the sectors/clusters of a particular file?
Hi, How to get the sectors/clusters of a particular file? I am working on Windows 2000. Hoping to get a reply soon. TIA. regards, Jahfer V P. No particular way I know of. If you have certain privileges you might be able to use DeviceIoControl(), but there isn't much you can do with this information. joe On Mon, 20 Jun 2005 23:33:02 -0700, "Jahfer V P" <JahferVP@discussions.microsoft.com> wrote: >Hi, > >How to get the sectors/clusters of a particular file? >I am working on Windows 2000. > >Hoping to get a reply soon. >TIA. > >regards, &g...

XSLT: Creating anchors from outer XML files
Hi, I'm trying to put the links (href attribute of the <a> element) out of my XSLT files, in a XML file, and read it from the stylesheet. I suppose that I have to use the document() function to link to the XML file containing the URLs, but I don't know how to do it. I've tried something like this: This is XML file containing the URLs (link.xml): <?xml version="1.0" encoding="utf-8" ?> <LINKS> <L1> /SomePage.aspx </L1> </LINKS> And this is the code from the stylesheet: <a href="{document('link.xml')/L...

XL2003 format of row- and column headers
Hello, how can I change the font of the headers of rows and columns in a sheet? Thanks for any help Wolfgang On Oct 3, 3:26=A0am, "Dr. Wolfgang Hintze" <w...@snafu.de> wrote: > Hello, > > how can I change the font of the headers of rows and columns in a > sheet? > > Thanks for any help > > Wolfgang Format > Style... > Normal > Modify ...

write protect/write redirect/kios mode
Hey, I LOVE snapshots in virtualmachines and think it would be awesome to have something like this for my main windows install. Anyone know of any hardware/software to redirect writes to another drive/file/memory/hardware? Or hardware to write protect a drive for kios's (i found some once befor the reverted all changes on shutdown, but cant find it anymore ><) I know VSS(virtual shadow copy service) has a function to redirect on write and wondering if i might be able to use that :/ It will just make everything cleaner, and give alot of flexiblity to setting up ...

Import Data From Landscape Format
I have a data set from an old Pascal program that was exported into this fomat: ID # : 10106 Sex : Female Raw Scores : 1 4 1 0 3 6 3 T Scores : 53 114 65 43 64 83 42 I need the headers to be ID#, Sex, Raw Scores 1-7 and T Scores 1-7 and then to pick up the data and place it in the appropriate cell. Is there a script that will accomplish this? Thank you for your help!! ...

How to replace two system files?
I have an XP machine. Have just replaced a couple files from a W7 machine. The files are:- c:\windows\system32\drivers\disk.sys c:\windows\system32\drivers\partmgr.sys ....and now I can't boot the XP machine. How to get back to before I replaced the files? Thanks. "John Smith" <JohnSmith@nospam.com> wrote in message news:uLL8mqL0KHA.264@TK2MSFTNGP05.phx.gbl... > I have an XP machine. Have just replaced a couple files from a W7 machine. > The files are:- > c:\windows\system32\drivers\disk.sys > c:\windows\system32\drivers\partmgr.sys > ....

How do I force sent mails to be plain text, not HTML?
Hi, In OE6 there is a handy feature "Reply to msgs using the format in which they where sent" in Tools - Options - Send. When I want to force each outgoing email to be sent as plain text, I just uncheck that (and select plain text as Mail Sending Format). AFAICT there is no way to do that in OE2000, ar am I missing something? TIA! Mike - Oops! Typo! Please read: "AFAICT there is no way to do that in OE2000, ar am I missing something?" - Cool! You cloaked your email addy! LOL "Jim Pickering" <&#106&#105&#109&#112&#64&#109&#118&a...

Cannot open .msg files saved from Outlook to network share
Some of my users have dragged and dropped emails into their network shares creating .msg files. When they try to open some of the files they get the following popup: 'The messaging interface has generated an unknown error. If the problem persists, restart Outlook.' This only happens for some of the saved emails. They are using Outlook 2000 from Office. God only knows why they did this and didn't use autoarchive! How many training sessions does someone have to do?! Anyway, anyone know how I can open them or even better, why I cant? Regards Dave ...

Time Formatting #2
Is there any way to format time to AM/PM without displaying AM/PM? Thanks. -- Brian Try the 24 hour clock Format>cells>time>13:30 "brianwakin" wrote: > Is there any way to format time to AM/PM without displaying AM/PM? Thanks. > -- > Brian The time needs to be displayed in a 12 hour clock format without the "AM or PM" actually being displayed. Is there any way to do this? Thanks again. -- Brian "Ray A" wrote: > Try the 24 hour clock > Format>cells>time>13:30 > > "brianwakin" wrote: > > > ...

Cannot write to memory location
I can not (all of the sudden) run Publisher in the last week. When starting to run it, I get that it cannot write to a certain memory locaiton, so it is shutting down. I need to get this program working. I have read through this forum and tried the different fixes as well as the knowledge base (emptied the temp file, disabled anti-virus, etc.) but it still won't work. An example is: "The instruction at "0x77f585c0" referenced memory at "0x00000000". the memory could not be "written". Click on OK to terminate the program." The header read...

fatal error LNK1104: cannot open file 'mfc71.lib'
I have a project that was built with VS 2003. After doing the auto conversion with VS 2005, I now get this error message when linking: fatal error LNK1104: cannot open file 'mfc71.lib' There is only one .cpp file in this project. There is no reference to MFC71.lib in the project. Any ideas? > I have a project that was built with VS 2003. After doing the auto > conversion with VS 2005, I now get this error message when linking: > > fatal error LNK1104: cannot open file 'mfc71.lib' > > There is only one .cpp file in this project. There is no reference to &g...

Temp file folder?
I'm using xp and OE6 I d/l'd aWHOLE bunch of encoded mp3files. During the compile and decode process my pc crashed. I suspect I have a WHOLE bunch of temp files somewhere. that I want to clean up. Any idea as to their location? Regards, Lawrence L Mossford <lmossford1@shaw.ca> wrote: > I'm using xp and OE6 Wrong newsgroup. > I d/l'd aWHOLE bunch of encoded mp3files. During the compile and > decode process my pc crashed. I suspect I have a WHOLE bunch of temp > files somewhere. that I want to clean up. Temporary Internet Files, perhaps? -- Brian...

text boxes
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) I can't seem to find any basic instructions on text boxes in Word for Mac 2008. Help menu is no good, and on mactopia I can only find answers to a few tricky specific problems. Could someone please direct me to where I can find text box 101? Thanks. This is about as close as you'll get [Unless Jim G comes along with page references in his Dummies book :-)]. It's from the Word 2003 Help but there's little � if any � difference in 2004/2008: <http://office.microsoft.com/en-us/word/HP052381641033.aspx?pid=CH0634...

Writing formula for excel worksheet
Item Rate1 Rate2 Rate3 A 3 4 6 B 8 5 23 C 56 5 78 Above is my excel sheet. I want to write some formula so that in every row cell having minimum rate should come bold. Like For Item A - Rate1(3) for Item B - Rate2(5) For Item C - Rate2 (5) There may be a neater way but this works. Somewhere in you sheet in a cell type =min(A1:a4), I used L1 Change this to watever range you are trying to evaluate. Then select this range and click format - conditional format - cell value - is equal to =($L$1) and apply your bold font. The lowest value in the range will then become bold. Mike "Gaur...

Change text with leading zeros into decimal
Hello, I am trying to change about 7,000 records right now, so I need som help that will not include manual changes. There is a column of tex data that needs to be changed into decimals; sounds easy, but many o them have leading zeros, which I need to stay. So I need the followin transfrmation: 0003456 into .0003456 Can anyone please help me !!! Thanks!!! -- Message posted from http://www.ExcelForum.com Hi try the following formula in an adjacent column (if A stores your values): =VALUE("0." & A1) copy this down Afgter this you can copy the entire helper column and use ...