473,238 Members | 1,772 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,238 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 3703
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.