473,397 Members | 1,960 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,397 software developers and data experts.

how to get this code to do exactly what I want ??

Ron
I've got some C# code that sort of does what I want:

Looking at the xml files in area:
http://www.keepitsimplekid.com/xml

I want to change Untitled Ad at the top of the xml to the name of the
etn advertiser name in this xml for example LEAF GUARD OF LAKE ERIE
or REGIONAL CANCER CENTER, whatever is in that ETS Advertiser I want
to move to Untitled Ad, right now as you can see by the code I hard
code LEAF GUARD OF LAKE ERIE into the code, how can I do it
dynamically? thanks for any help..

code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
String[] myFiles = System.IO.Directory.GetFiles("C:/
WAMP/", "*.xml", System.IO.SearchOption.TopDirectoryOnly);

// Show how many files were found
tb_Log.Text = myFiles.Length.ToString() + " files
acquired.";

if (MessageBox.Show("Would you like to continue?",
"Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Show all files that match
foreach (string fileName in myFiles)
{
// Create our streamreader
StreamReader myStream = new
StreamReader(fileName);

// Loads file contents into string
string myDocument = myStream.ReadToEnd();

// Replaces Untitled Ad with new text
myDocument = myDocument.Replace("Untitled Ad",
"LEAF GUARD OF LAKE ERIE");

// Releases the file so we can overwrite it
myStream.Close();

// Creates a new streamwriter for the file
StreamWriter myNewStream = new
StreamWriter(fileName, false);

// Writes new text (with replacements) to stream
myNewStream.Write(myDocument);

// Saves changes to file and closes
myNewStream.Close();
}

// Print our completion
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...conversion completed";
}
else
{
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...process aborted";
}
}
}
}

May 14 '07 #1
5 1424
On May 14, 7:03 am, Ron <pts4...@yahoo.comwrote:
I've got some C# code that sort of does what I want:

Looking at the xml files in area:http://www.keepitsimplekid.com/xml

I want to change Untitled Ad at the top of the xml to the name of the
etn advertiser name in this xml for example LEAF GUARD OF LAKE ERIE
or REGIONAL CANCER CENTER, whatever is in that ETS Advertiser I want
to move to Untitled Ad, right now as you can see by the code I hard
code LEAF GUARD OF LAKE ERIE into the code, how can I do it
dynamically? thanks for any help..

code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
String[] myFiles = System.IO.Directory.GetFiles("C:/
WAMP/", "*.xml", System.IO.SearchOption.TopDirectoryOnly);

// Show how many files were found
tb_Log.Text = myFiles.Length.ToString() + " files
acquired.";

if (MessageBox.Show("Would you like to continue?",
"Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Show all files that match
foreach (string fileName in myFiles)
{
// Create our streamreader
StreamReader myStream = new
StreamReader(fileName);

// Loads file contents into string
string myDocument = myStream.ReadToEnd();

// Replaces Untitled Ad with new text
myDocument = myDocument.Replace("Untitled Ad",
"LEAF GUARD OF LAKE ERIE");

// Releases the file so we can overwrite it
myStream.Close();

// Creates a new streamwriter for the file
StreamWriter myNewStream = new
StreamWriter(fileName, false);

// Writes new text (with replacements) to stream
myNewStream.Write(myDocument);

// Saves changes to file and closes
myNewStream.Close();
}

// Print our completion
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...conversion completed";
}
else
{
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...process aborted";
}
}
}

}
Hi,

you can use the SelectNodes methods (with a given XPath expression) to
get a list of nodes that contains the desired information. Then, for
each of the nodes, set the InnerText property to the replaced text.

Feel free to ask any other question.

Hope this helps.

Moty.

May 14 '07 #2
On May 14, 7:03 am, Ron <pts4...@yahoo.comwrote:
I've got some C# code that sort of does what I want:

Looking at the xml files in area:http://www.keepitsimplekid.com/xml

I want to change Untitled Ad at the top of the xml to the name of the
etn advertiser name in this xml for example LEAF GUARD OF LAKE ERIE
or REGIONAL CANCER CENTER, whatever is in that ETS Advertiser I want
to move to Untitled Ad, right now as you can see by the code I hard
code LEAF GUARD OF LAKE ERIE into the code, how can I do it
dynamically? thanks for any help..

code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
String[] myFiles = System.IO.Directory.GetFiles("C:/
WAMP/", "*.xml", System.IO.SearchOption.TopDirectoryOnly);

// Show how many files were found
tb_Log.Text = myFiles.Length.ToString() + " files
acquired.";

if (MessageBox.Show("Would you like to continue?",
"Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Show all files that match
foreach (string fileName in myFiles)
{
// Create our streamreader
StreamReader myStream = new
StreamReader(fileName);

// Loads file contents into string
string myDocument = myStream.ReadToEnd();

// Replaces Untitled Ad with new text
myDocument = myDocument.Replace("Untitled Ad",
"LEAF GUARD OF LAKE ERIE");

// Releases the file so we can overwrite it
myStream.Close();

// Creates a new streamwriter for the file
StreamWriter myNewStream = new
StreamWriter(fileName, false);

// Writes new text (with replacements) to stream
myNewStream.Write(myDocument);

// Saves changes to file and closes
myNewStream.Close();
}

// Print our completion
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...conversion completed";
}
else
{
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...process aborted";
}
}
}

}
Further more, much more powerful solution:

Using XmlDocument you can create an editable XPathNavigator to edit
values in your document.

MSDN Example:

XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new
XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

foreach (XPathNavigator nav in navigator.Select("//bk:price",
manager))
{
if(nav.Value == "11.99")
{
nav.SetValue("12.99");
}
}

Console.WriteLine(navigator.OuterXml);

Hope this helps =)

Moty
May 14 '07 #3
Ron
Could someone post a workable solution that I could try in my code?
I've never used any of these methods and don't really know where to
begin with them.
On May 14, 1:52 am, Moty Michaely <Moty...@gmail.comwrote:
On May 14, 7:03 am, Ron <pts4...@yahoo.comwrote:


I've got some C# code that sort of does what I want:
Looking at the xml files in area:http://www.keepitsimplekid.com/xml
I want to change Untitled Ad at the top of the xml to the name of the
etn advertiser name in this xml for example LEAF GUARD OF LAKE ERIE
or REGIONAL CANCER CENTER, whatever is in that ETS Advertiser I want
to move to Untitled Ad, right now as you can see by the code I hard
code LEAF GUARD OF LAKE ERIE into the code, how can I do it
dynamically? thanks for any help..
code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
String[] myFiles = System.IO.Directory.GetFiles("C:/
WAMP/", "*.xml", System.IO.SearchOption.TopDirectoryOnly);
// Show how many files were found
tb_Log.Text = myFiles.Length.ToString() + " files
acquired.";
if (MessageBox.Show("Would you like to continue?",
"Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Show all files that match
foreach (string fileName in myFiles)
{
// Create our streamreader
StreamReader myStream = new
StreamReader(fileName);
// Loads file contents into string
string myDocument = myStream.ReadToEnd();
// Replaces Untitled Ad with new text
myDocument = myDocument.Replace("Untitled Ad",
"LEAF GUARD OF LAKE ERIE");
// Releases the file so we can overwrite it
myStream.Close();
// Creates a new streamwriter for the file
StreamWriter myNewStream = new
StreamWriter(fileName, false);
// Writes new text (with replacements) to stream
myNewStream.Write(myDocument);
// Saves changes to file and closes
myNewStream.Close();
}
// Print our completion
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...conversion completed";
}
else
{
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...process aborted";
}
}
}
}

Further more, much more powerful solution:

Using XmlDocument you can create an editable XPathNavigator to edit
values in your document.

MSDN Example:

XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new
XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

foreach (XPathNavigator nav in navigator.Select("//bk:price",
manager))
{
if(nav.Value == "11.99")
{
nav.SetValue("12.99");
}

}

Console.WriteLine(navigator.OuterXml);

Hope this helps =)

Moty- Hide quoted text -

- Show quoted text -

May 14 '07 #4
On May 14, 9:12 am, Ron <pts4...@yahoo.comwrote:
Could someone post a workable solution that I could try in my code?
I've never used any of these methods and don't really know where to
begin with them.

On May 14, 1:52 am, Moty Michaely <Moty...@gmail.comwrote:
On May 14, 7:03 am, Ron <pts4...@yahoo.comwrote:
I've got some C# code that sort of does what I want:
Looking at the xml files in area:http://www.keepitsimplekid.com/xml
I want to change Untitled Ad at the top of the xml to the name of the
etn advertiser name in this xml for example LEAF GUARD OF LAKE ERIE
or REGIONAL CANCER CENTER, whatever is in that ETS Advertiser I want
to move to Untitled Ad, right now as you can see by the code I hard
code LEAF GUARD OF LAKE ERIE into the code, how can I do it
dynamically? thanks for any help..
code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
String[] myFiles = System.IO.Directory.GetFiles("C:/
WAMP/", "*.xml", System.IO.SearchOption.TopDirectoryOnly);
// Show how many files were found
tb_Log.Text = myFiles.Length.ToString() + " files
acquired.";
if (MessageBox.Show("Would you like to continue?",
"Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Show all files that match
foreach (string fileName in myFiles)
{
// Create our streamreader
StreamReader myStream = new
StreamReader(fileName);
// Loads file contents into string
string myDocument = myStream.ReadToEnd();
// Replaces Untitled Ad with new text
myDocument = myDocument.Replace("Untitled Ad",
"LEAF GUARD OF LAKE ERIE");
// Releases the file so we can overwrite it
myStream.Close();
// Creates a new streamwriter for the file
StreamWriter myNewStream = new
StreamWriter(fileName, false);
// Writes new text (with replacements) to stream
myNewStream.Write(myDocument);
// Saves changes to file and closes
myNewStream.Close();
}
// Print our completion
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...conversion completed";
}
else
{
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...process aborted";
}
}
}
}
Further more, much more powerful solution:
Using XmlDocument you can create an editable XPathNavigator to edit
values in your document.
MSDN Example:
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new
XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");
foreach (XPathNavigator nav in navigator.Select("//bk:price",
manager))
{
if(nav.Value == "11.99")
{
nav.SetValue("12.99");
}
}
Console.WriteLine(navigator.OuterXml);
Hope this helps =)
Moty- Hide quoted text -
- Show quoted text -
Since the files you are working with are XML files, it's assembled
with tags (nodes). Each node has attributes. To achieve what you want,
you need to know where to look for the data you want to replace
(Meaning, attribute or element name).

Is that attribute or element constant?

The example above is simple enough to do what you need. Check MSDN
about the Select method of the XPathNavigator to select all the nodes
with the element name you wish to replace, and that's all.

Feel free to ask any other question.

Moty =)
May 14 '07 #5
Ron
Based on the 2 sample xml docs at: http://www.keepitsimplekid.com/xml
that is the layout of all the xml docs. I've tried to work with the
suggestions you have given with no luck, I'll try to find some more
on these topics of xpathnavigator

thanks

On May 14, 2:33 am, Moty Michaely <Moty...@gmail.comwrote:
On May 14, 9:12 am, Ron <pts4...@yahoo.comwrote:
Could someone post a workable solution that I could try in my code?
I've never used any of these methods and don't really know where to
begin with them.
On May 14, 1:52 am, Moty Michaely <Moty...@gmail.comwrote:
On May 14, 7:03 am, Ron <pts4...@yahoo.comwrote:
I've got some C# code that sort of does what I want:
Looking at the xml files in area:http://www.keepitsimplekid.com/xml
I want to change Untitled Ad at the top of the xml to the name of the
etn advertiser name in this xml for example LEAF GUARD OF LAKE ERIE
or REGIONAL CANCER CENTER, whatever is in that ETS Advertiser I want
to move to Untitled Ad, right now as you can see by the code I hard
code LEAF GUARD OF LAKE ERIE into the code, how can I do it
dynamically? thanks for any help..
code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
String[] myFiles = System.IO.Directory.GetFiles("C:/
WAMP/", "*.xml", System.IO.SearchOption.TopDirectoryOnly);
// Show how many files were found
tb_Log.Text = myFiles.Length.ToString() + " files
acquired.";
if (MessageBox.Show("Would you like to continue?",
"Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Show all files that match
foreach (string fileName in myFiles)
{
// Create our streamreader
StreamReader myStream = new
StreamReader(fileName);
// Loads file contents into string
string myDocument = myStream.ReadToEnd();
// Replaces Untitled Ad with new text
myDocument = myDocument.Replace("Untitled Ad",
"LEAF GUARD OF LAKE ERIE");
// Releases the file so we can overwrite it
myStream.Close();
// Creates a new streamwriter for the file
StreamWriter myNewStream = new
StreamWriter(fileName, false);
// Writes new text (with replacements) to stream
myNewStream.Write(myDocument);
// Saves changes to file and closes
myNewStream.Close();
}
// Print our completion
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...conversion completed";
}
else
{
tb_Log.Text += Environment.NewLine;
tb_Log.Text += "...process aborted";
}
}
}
}
Further more, much more powerful solution:
Using XmlDocument you can create an editable XPathNavigator to edit
values in your document.
MSDN Example:
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new
XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");
foreach (XPathNavigator nav in navigator.Select("//bk:price",
manager))
{
if(nav.Value == "11.99")
{
nav.SetValue("12.99");
}
}
Console.WriteLine(navigator.OuterXml);
Hope this helps =)
Moty- Hide quoted text -
- Show quoted text -

Since the files you are working with are XML files, it's assembled
with tags (nodes). Each node has attributes. To achieve what you want,
you need to know where to look for the data you want to replace
(Meaning, attribute or element name).

Is that attribute or element constant?

The example above is simple enough to do what you need. Check MSDN
about the Select method of the XPathNavigator to select all the nodes
with the element name you wish to replace, and that's all.

Feel free to ask any other question.

Moty =)

May 14 '07 #6

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

Similar topics

22
by: Martin MOKREJ© | last post by:
Hi, I'm looking for some easy way to do something like include in c or PHP. Imagine I would like to have: cat somefile.py a = 222 b = 111 c = 9
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
21
by: Sandy | last post by:
Hello - I am using Visual Studio .Net. I need an example of how to construct a class that can be used throughout a project where I can include its subs and functions in various pages in the...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
26
by: webrod | last post by:
Hi, I have some php pages with a lot of HTML code. I am looking for a HTML validator tool (like TIDY). TIDY is not good enough with PHP tags (it removes a lot of php code). Do you have any...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
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
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?
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.