How can I fix this code??

  • Thread starter Thread starter Ron
  • Start date Start date
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.?
 
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);
 
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);
 
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);
 
Back
Top