Click here to Skip to main content
15,887,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I successfully imported Background value from XML file to Textbox txtInputFileT, however I still need to import value of X0 into NumericUpDown NumericUpDownX0 and value of cmbFontSize0 to Combobox ComboboxX0

Not sure how to structure it within the while and if statements ?


My XML file:
<?xml version="1.0"?>
<ArrayOfXMLSaveClass xmlns:xsi="http://www.w3.org/2001/XMLSchema">
  <XMLSaveClass>

    <Background>D:\Temp\100 Hacks for Every Occasion.pdf</Background>
    <X0>4.5</X0>
    <cmbFontSize0>12</cmbFontSize0>
  
</XMLSaveClass>
</ArrayOfXMLSaveClass>


What I have tried:

        private void button4_Click(object sender, EventArgs e)
        {           
            var dialog = new OpenFileDialog
            {
                Filter = "XML (*.xml)|*.xml"
            };

            if (dialog.ShowDialog() != DialogResult.OK)
            return;

            var streamX = dialog.OpenFile();                   
            var readerX = XmlReader.Create(streamX);


            while (readerX.Read())
            {
                if (readerX.NodeType != XmlNodeType.Element || readerX.Name != "Background")
                    continue;

                var L01 = readerX.ReadElementContentAsString();

                    try
                    {                        
                        txtInputFileT.Text = L01;
                        XX.Enabled = true;
                        YY.Enabled = true;
                        UpdatePreview();
                    }
                    catch (Exception Er3)
                    {
                    MessageBox.Show(Er3.Message);
                    }
					
            }
         }
Posted
Updated 25-Feb-21 19:39pm
v3
Comments
[no name] 24-Feb-21 16:42pm    
Load your values into an object / properties and bind to that; don't stuff controls directly ... because of the issues you're encountering.
Xlance 25-Feb-21 0:45am    
Could you please give an example. Thank you

1 solution

May be it's not optimal but that's the best I could do !
var streamX02 = dialog.OpenFile();
var streamX03 = dialog.OpenFile();


var readerX02 = XmlReader.Create(streamX02);
var readerX03 = XmlReader.Create(streamX03);


while (readerX02.Read())
{
    if (readerX02.NodeType != XmlNodeType.Element || readerX02.Name != "txtInputFile0")
        continue;
    var L02 = readerX02.ReadElementContentAsString();
    try
    {
        txtInputFile0.Text = L02;
        //UpdatePreview();
    }
    catch (Exception Er3)
    {
        //Block of code to handle errors
    }
}


while (readerX03.Read())
{
    if (readerX03.NodeType != XmlNodeType.Element || readerX03.Name != "cmbFont0")
        continue;
    var L03 = readerX03.ReadElementContentAsString();
    try
    {
        cmbFont0.Text = L03;
        //UpdatePreview();
    }
    catch (Exception Er3)
    {
        //Block of code to handle errors
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900