to to fix this can not convert string error??

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

I think this code is almost working but I get the following error:
Error 1 Cannot convert type 'string' to 'System.Xml.XmlAttribute' C:
\Spring 2007\C#\convertxml\convertxml\Form1.cs 35 15 convertxml

ths is what is underlined in blue:
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/@NAME").Value =
newvalue;

this is my whole 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);

string newvalue =
((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item/
@NAME")).Value.tostring;
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/
@NAME").Value = newvalue;

xdoc.Save(fileName);
}
}

How can I fix this error?
 
I'm not sure I understand the whole thing, but in one place I see

..tostring

when you almost certainly meant:

..ToString()

Is that it?
 
The LHS of the assignment is cast as XmlAttribute butt the RHS is a string.
Obviously a string cannot be assigned to an XmlAttribute.
 
I think this code is almost working but I get the following error:
Error 1 Cannot convert type 'string' to 'System.Xml.XmlAttribute' C:
\Spring 2007\C#\convertxml\convertxml\Form1.cs 35 15 convertxml

ths is what is underlined in blue:
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/@NAME").Value =
newvalue;

this is my whole 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);

string newvalue =
((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item/
@NAME")).Value.tostring;
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/
@NAME").Value = newvalue;

xdoc.Save(fileName);
}
}

How can I fix this error?

Remove the (XmlAttribute) from that line and see if it works. I think
it's unnecessary. The compiler is trying to convert the entire
xdoc.SelectSingleNode("/XMD-entity/Meta/
@NAME").Value (which is String) into an xmlAttribute, which is impossible.

Freiddie
http://fei.yuanbw.googlepages.com/
http://freiddy.blogspot.com/
http://scilearn.blogspot.com/
 
I think this code is almost working but I get the following error:
Error 1 Cannot convert type 'string' to 'System.Xml.XmlAttribute' C:
\Spring 2007\C#\convertxml\convertxml\Form1.cs 35 15 convertxml

ths is what is underlined in blue:
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/@NAME").Value =
newvalue;

this is my whole 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);

string newvalue =
((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item/
@NAME")).Value.tostring;
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/
@NAME").Value = newvalue;

xdoc.Save(fileName);
}
}

How can I fix this error?

Try adding these brackets.
((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/
@NAME")).Value = newvalue;

Freiddie
http://fei.yuanbw.googlepages.com/
http://freiddy.blogspot.com/
http://scilearn.blogspot.com/
 
yes, I did mean .ToString()

this does not help though.

How can I fix it so that this works? Any idea?

The LHS of the assignment is cast as XmlAttribute butt the RHS is a string.
Obviously a string cannot be assigned to an XmlAttribute.

Ron said:
I think this code is almost working but I get the following error:
Error 1 Cannot convert type 'string' to 'System.Xml.XmlAttribute' C:
\Spring 2007\C#\convertxml\convertxml\Form1.cs 35 15 convertxml
ths is what is underlined in blue:
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/@NAME").Value =
newvalue;
this is my whole 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);
string newvalue =
((XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Application_Data/
Application_Info[@AI_TYPE='ETS_ADVERTISER']/Ai_Item/
@NAME")).Value.tostring;
(XmlAttribute)xdoc.SelectSingleNode("/XMD-entity/Meta/
@NAME").Value = newvalue;
xdoc.Save(fileName);
}
}

How can I fix this error?
 
Back
Top