473,406 Members | 2,217 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.

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
Nov 12 '05 #1
3 3709
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" <Ra******@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.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

Nov 12 '05 #2
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" <Ra******@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.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


Nov 12 '05 #3
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" <Ra******@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.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" <Ra******@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.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


Nov 12 '05 #4

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

Similar topics

3
by: Ian Davies | last post by:
I have the following code which displays some some formatted text in my browser. The problem is I need to create a text file and write the records from this array into it. Does anyone know how to...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
2
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over...
10
by: Aaron | last post by:
Hello, I have a small application that I need to save data from 7 text boxes in to a csv file. This will entail btnNext_Click function that will create a new csv file and enter the 7 data fields...
1
by: Tomas Vera | last post by:
Hello All, I'm having trouble writing some special characters to an XML file. I need to output a XMLSS formatted file to be read by Excel. In some cells there will be some text that contains...
7
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
1
by: RahimAsif | last post by:
I am designing a data acquisition program that peridically collects data from a panel and writes to a file. The way I am doing it right now, every time I have data from the panel, I open the file,...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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
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.