How can I fix this code??

R

Ron

I have this code:
private void btnConvert_Click(object sender, EventArgs e)
{
// Get a directory
string path = @"c:\WAMP\";


foreach (string fileName in Directory.GetFiles(path,
"*.xml"))
{

XmlDocument xdoc = new XmlDocument();

xdoc.Load(fileName);

((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/
@name")).Value = "new value";

((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/
Application_Data/Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item/
@name")).Value = "new value";

xdoc.Save(fileName);
}
}

and I am working with the .xml files in this directory:
http://www.keepitsimplekid.com/xml

I want the text Untitled Ad in these xml docs to be replaced with the
Name of the ETS Advertiserr, in these examples LEAF GUARD OF LAKE ERIE
and REGIONAL CANCER CENTER

Now this code takes new value and inserts that text in the ETS
Advertiser name and also replaces Untitled Ad with new value.

How can I modify this code to read whatever is in ETS ADVERTISER and
then replace Untitled Ad with the text it gets from there.?
 
M

Marcin Hoppe

Now this code takes new value and inserts that text in the ETS
Advertiser name and also replaces Untitled Ad with new value.

How can I modify this code to read whatever is in ETS ADVERTISER and
then replace Untitled Ad with the text it gets from there.?

I guess that what you have a problem with is not reading finding files
but manipulating XML document. You are quite close with your solution
but I find it quite hard to read. Here is a solution to your problem
written in a slighly more readable fashion:

const string DocumentPath = @"C:\Ad00304.xml";
const string DocumentCopyPath = @"C:\Ad00304_Copy.xml";
const string EtsAdvertiserXPath = "/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item";
const string MetaXPath = "/XMD-entity/Meta";

XmlDocument doc = new XmlDocument();
doc.Load(DocumentPath);

XmlNode etsAdvertiserItemNode =
doc.SelectSingleNode(EtsAdvertiserXPath);
string etsAdvertieserName =
etsAdvertiserItemNode.Attributes["NAME"].InnerText;

XmlNode metaNode = doc.SelectSingleNode(MetaXPath);
metaNode.Attributes["NAME"].InnerText = etsAdvertieserName;

doc.Save(DocumentCopyPath);
 
R

Ron

When I run this I get a bunch of errors. Is there a way though for me
to look at all .xml files in the directory c:\convert?

would I just do:
const string DocumentPath = @"C:\convert\*.xml";
const string DocumentCopyPath = @"C:\convert\*_Copy.xml";

? Would this work?

Now this code takes new value and inserts that text in the ETS
Advertiser name and also replaces Untitled Ad with new value.
How can I modify this code to read whatever is in ETS ADVERTISER and
then replace Untitled Ad with the text it gets from there.?

I guess that what you have a problem with is not reading finding files
but manipulating XML document. You are quite close with your solution
but I find it quite hard to read. Here is a solution to your problem
written in a slighly more readable fashion:

const string DocumentPath = @"C:\Ad00304.xml";
const string DocumentCopyPath = @"C:\Ad00304_Copy.xml";
const string EtsAdvertiserXPath = "/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item";
const string MetaXPath = "/XMD-entity/Meta";

XmlDocument doc = new XmlDocument();
doc.Load(DocumentPath);

XmlNode etsAdvertiserItemNode =
doc.SelectSingleNode(EtsAdvertiserXPath);
string etsAdvertieserName =
etsAdvertiserItemNode.Attributes["NAME"].InnerText;

XmlNode metaNode = doc.SelectSingleNode(MetaXPath);
metaNode.Attributes["NAME"].InnerText = etsAdvertieserName;

doc.Save(DocumentCopyPath);
 
B

Ben Voigt

Ron said:
When I run this I get a bunch of errors. Is there a way though for me
to look at all .xml files in the directory c:\convert?

would I just do:
const string DocumentPath = @"C:\convert\*.xml";
const string DocumentCopyPath = @"C:\convert\*_Copy.xml";

no, but see foreach and DirectoryInfo.GetFiles()
? Would this work?

Now this code takes new value and inserts that text in the ETS
Advertiser name and also replaces Untitled Ad with new value.
How can I modify this code to read whatever is in ETS ADVERTISER and
then replace Untitled Ad with the text it gets from there.?

I guess that what you have a problem with is not reading finding files
but manipulating XML document. You are quite close with your solution
but I find it quite hard to read. Here is a solution to your problem
written in a slighly more readable fashion:

const string DocumentPath = @"C:\Ad00304.xml";
const string DocumentCopyPath = @"C:\Ad00304_Copy.xml";
const string EtsAdvertiserXPath = "/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item";
const string MetaXPath = "/XMD-entity/Meta";

XmlDocument doc = new XmlDocument();
doc.Load(DocumentPath);

XmlNode etsAdvertiserItemNode =
doc.SelectSingleNode(EtsAdvertiserXPath);
string etsAdvertieserName =
etsAdvertiserItemNode.Attributes["NAME"].InnerText;

XmlNode metaNode = doc.SelectSingleNode(MetaXPath);
metaNode.Attributes["NAME"].InnerText = etsAdvertieserName;

doc.Save(DocumentCopyPath);
 

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