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

R

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";
}
}
}
}
 
M

Moty Michaely

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.
 
M

Moty Michaely

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
 
R

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.


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 -
 
M

Moty Michaely

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.

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");
}


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 =)
 
R

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

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.
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 =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top