Resolved cannot convert from string to System.Xml.XmlNode error

ram_rocks

Active member
Joined
Jun 14, 2021
Messages
27
Programming Experience
1-3
In the below code I am getting error with the statement inside the for loop so here basically I am trying to get all the specific tag name I want and copy all of them into new xml document. Can anyone please help me out to resolve this issue
C#:
XmlDocument _xd = new XmlDocument();

_xd.Load(@"C:\rsr.xml");

XmlNodeList elemList = _xd.GetElementsByTagName("Prototype");

XmlDocument ifArxml = new XmlDocument();

XmlElement rootnode = ifArxml.CreateElement("TS");

XmlElement arpackgs = ifArxml.CreateElement("context");

XmlElement arpackg = ifArxml.CreateElement("name");

XmlElement snm = ifArxml.CreateElement("package");

XmlElement elem = ifArxml.CreateElement("elements");

ifArxml.AppendChild(rootnode);
rootnode.AppendChild(arpackgs);
arpackgs.AppendChild(arpackg);
arpackg.AppendChild(snm);
arpackg.AppendChild(elem);

for (int i = 0; i < elemList.Count; i++)
{
    elem.AppendChild(elemList.OuterXml);
}

ifArxml.Save(@"C:\check.xml");
 
Last edited by a moderator:
Is that really the code you're running? I doubt it. Firstly, in order to format your code, I had to add a closing double-quote to one of the arguments provide to CreateElement, so that wouldn't have compiled. Secondly, I think that the line you're claiming is the problem is not your actual code either. That code is supposedly getting the OuterXml property of an XmlNodeList object, but there is no such property. I would assume that your code is actually this:
C#:
elem.AppendChild(elemList[i].OuterXml);
It's important that you post the actual code you're having issues with because I just had to waste time working this all out before I could even consider addressing the problem. If at all possible, copy your code directly from the code editor and paste it here, so you know it's the actual code you're using. If you can't do that, be VERY careful when typing it out.
 
As for the issue, the error message is telling you exactly what's wrong, so what's the problem. You have an XmlNode and you get its OuterXml, which is a string, and you then pass that to the XmlElement.AppendChild method, which is expecting an XmlNode object. You can't provide a string where an XmlNode is expected, exactly as the error message is telling you.
 
I apologize for placing the wrong line (27) yes you are correct it is elem.AppendChild(elemList.OuterXml)
I am new to C# and learning the stuff from google. I am trying to figure out the solution for above issue and as you mentioned yes it is expecting a XmlNode object and I am not able to figure it out how to resolve it. Please share your idea or else let me know the best way to do it.
 
If you pass in an elemList[i] then you are passing in an XmlNode. While learning C#, you should have learned about inheritance. An XmlElement inherits from XmlNode. Also while learning C#, you should have learned about reading the documentation. In the documentation, you would have discovered this inheritance structure.


Learning C# via Google is non-optimal. You should take time to pickup a book, or go through a C# tutorial. (I also don't recommend learning C# via YouTube.)
 
Back
Top Bottom