I've been trying for days to read a value from XML file into a text box. I searched relevant questions:
Reading values from xml file with Linq
C# Reading from XML files
Getting values from xml file using C#
How to Read values from XML file
and videos:
https://www.youtube.com/watch?v=4dPWkEARptI
https://www.youtube.com/watch?v=-DwANN5_BoE
Still, I couldn't correctly read the "Background" value and always get this Error: (I doubt it has to do with hierarchy of the XML file and I'm not choosing the correct Element \ Node) What did I miss?
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: 'None' is an invalid XmlNodeType.
My XML file:
<?xml version="1.0"?>
<ArrayOfXMLSaveClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<XMLSaveClass>
<Background>D:\Temp\100 Ideas for Every Occasion.pdf</Background>
</XMLSaveClass>
My code so far:
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog OP = new OpenFileDialog();
OP.Filter = "XML files (*.xml)|*.xml";
OP.DefaultExt = "xml";
OP.AddExtension = true;
if (OP.ShowDialog() == DialogResult.OK)
{
XmlReader XDoc = XmlReader.Create(OP.Filename);
while (XDoc.Read());
{
if (XDoc.NodeType == XmlNodeType.Element && XDoc.Name == "Background")
{
}
}
string L01 = XDoc.ReadElementString();
txtInputFileT.Text = L01;
}
}


ArrayOfXMLSaveClasshas no closing tag. Anyway why do you parse the xml yourself node by node? Why not uselinq2Xmlor anXmlSerializer?