Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML serialization
I have im my design a Tri State TreeView for which I create a xml file to serialize. I create my xsd schmea and then i have generated my class for that schema (workflow.xml -> workflow.xsd -> workflow.cs)
 
I want to parse in xml values "1" or "0" if a box from Tri State is checked or not
 
My code looks like that:
 
( WorkflowModulSplitAnalizaSplit() and WorkflowModulCapturaPrelucrareCaptura() are classes generated in workflow.cs )
  private void WriteXMLFileUsingValuesFromNodes(string filenamexml)
        {
              WorkflowModulSplitAnalizaSplit mc_s = new WorkflowModulSplitAnalizaSplit();            
              if (triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[0]) == TriStateTreeView.CheckState.Checked)
              {
                  mc_s.M0_RevertSnapshot = byte.Parse("1");                
              }
              else
              {
                  mc_s.M0_RevertSnapshot = byte.Parse("0");                
              }
              if (triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[1]) == TriStateTreeView.CheckState.Checked)
              {
                  mc_s.M0_PowerOnVM = byte.Parse("1");
              }
              else
              {
                  mc_s.M0_PowerOnVM = byte.Parse("0");
              }
                    
 
              XmlSerializer xs_s = new XmlSerializer(typeof(WorkflowModulCapturaPrelucrareCaptura));
              FileStream fs_s = new FileStream(@"C:\Users\SmartAdm\Documents\visual studio 2012\Projects\Workflow_Robo\Workflow_Robo\workflow.xml", FileMode.Create);
              xs_s.Serialize(fs_s, mc_s);
              fs_s.Close();    
        //=============================================================================================  
             
                WorkflowModulCapturaPrelucrareCaptura mc = new WorkflowModulCapturaPrelucrareCaptura();
                if (triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[1].Nodes[1].Nodes[12]) == TriStateTreeView.CheckState.Checked)
                {
                    mc.M3_CopyResultonserver = byte.Parse("1");
                }
                else
                {
                    mc.M3_CopyResultonserver = byte.Parse("0");
                }
                
                if (triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[1].Nodes[1].Nodes[0]) == TriStateTreeView.CheckState.Checked)
                {
                    mc.M3_RevertSnapshot = byte.Parse("1");
                }
                else
                {
                    mc.M3_RevertSnapshot = byte.Parse("0");
                }
 
                XmlSerializer xs = new XmlSerializer(typeof(WorkflowModulCapturaPrelucrareCaptura));
                FileStream fs = new FileStream(@"C:\Users\SmartAdm\Desktop\Workflow_Robo\Workflow_Robo\workflow.xml", FileMode.Create);                
                xs.Serialize(fs, mc);
                fs.Close();          
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            this.Refresh();
        }
 
        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {            
            string name = saveFileDialog1.FileName;            
            WriteXMLFileUsingValuesFromNodes(name);
        }

 
It only writes one node from xml and I lose the other nodes. It is a way in which I can adapt my code so it will write in all nodes from xml.
 
Thank you very much!
Posted 20-Feb-13 22:42pm
belea17320
Edited 20-Feb-13 23:23pm
CHill6067.6K
v2
Comments
Jegan Thiyagesan at 21-Feb-13 6:57am
   
Hi,
On the first section of the code your serialising
 
WorkflowModulSplitAnalizaSplit mc_s = new WorkflowModulSplitAnalizaSplit();
 
as a type of
 
XmlSerializer xs_s = new XmlSerializer(typeof(WorkflowModulCapturaPrelucrareCaptura));
 
I am not sure the serializer will translate this correctly.
 
Also you could optimize the if statement as follows:
 
mc.M3_CopyResultonserver = Convert.ToByte( triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[1].Nodes[1].Nodes[12]));
 
Regards
Jegan
belea17 at 21-Feb-13 7:01am
   
yes, i see your point but that dose not resolve my problem. I need some more! :)
Jegan Thiyagesan at 21-Feb-13 10:47am
   
Which part actually get written to the xml file and which part doesn't? you seems to have two objects writing to two different files. Is both files writing the first node only i.e. mc_s.M0_RevertSnapshot on the first file and mc.M3_CopyResultonserver on the second file?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If I run my code as it is, it will write the last part, from below code, for mc object
 

WorkflowModulCapturaPrelucrareCaptura mc = new WorkflowModulCapturaPrelucrareCaptura();
if (triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[1].Nodes[1].Nodes[12]) == TriStateTreeView.CheckState.Checked)
{
mc.M3_CopyResultonserver = byte.Parse("1");
}
else
{
mc.M3_CopyResultonserver = byte.Parse("0");
}

if (triStateTreeView1.GetChecked(triStateTreeView1.Nodes[0].Nodes[1].Nodes[1].Nodes[0]) == TriStateTreeView.CheckState.Checked)
{
mc.M3_RevertSnapshot = byte.Parse("1");
}
else
{
mc.M3_RevertSnapshot = byte.Parse("0");
}

XmlSerializer xs = new XmlSerializer(typeof(WorkflowModulCapturaPrelucrareCaptura));
FileStream fs = new FileStream(@"C:\Users\SmartAdm\Desktop\Workflow_Robo\Workflow_Robo\workflow.xml", FileMode.Create);
xs.Serialize(fs, mc);
fs.Close();
 
If I comment that part of code it will be written the first part, for object mc_s...
 
My xml file looks like that:



1
1
1
1


1
1
1
1




1
1
1
1

1 1 1 1


 
I want to parse value "0" or "1" to elements from xml Smile | :)
  Permalink  
v2
Comments
Jegan Thiyagesan at 22-Feb-13 8:29am
   
Your mc_s data is written to file "C:\Users\SmartAdm\Documents\visual studio 2012\Projects\Workflow_Robo\Workflow_Robo\workflow.xml"
 
and your mc data is written to "C:\Users\SmartAdm\Desktop\Workflow_Robo\Workflow_Robo\workflow.xml"
 
These are two different location, so your xml file will show only one data set.
 
Also can you post the xml data with elements rather than just 1's?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
[Serializable]
[XmlRoot("RootElement")]
public class RootElement
{
        [XmlElement]
        public WorkflowModulSplitAnalizaSplit ModulSplitAnalizaSplit { get; set; }
        public WorkflowModulCapturaPrelucrareCaptura ModulCapturaPrelucrareCaptura { get; set;}
}
 
public class WorkflowModulSplitAnalizaSplit
{
        [XmlElement]
        public byte M0_RevertSnapshot { get; set; }
        public byte M0_PowerOnVM { get; set; }
}
 
public class WorkflowModulCapturaPrelucrareCaptura
{
        [XmlElement]
        public byte M3_CopyResultonserver { get; set; }
        public byte M3_RevertSnapshot { get; set; }
}
 

public void WriteXMLFileUsingValuesFromNodes()
        {
            WorkflowModulSplitAnalizaSplit mc_s = new WorkflowModulSplitAnalizaSplit();
 
            mc_s.M0_RevertSnapshot = Convert.ToByte(1);
            mc_s.M0_PowerOnVM = Convert.ToByte(0);
 
            XmlSerializer xs_s = new XmlSerializer(typeof(WorkflowModulSplitAnalizaSplit));
            string fileName_1 = "xmlTest_1.xml";
            using (TextWriter tw = new StreamWriter(fileName_1))
            {
                xs_s.Serialize(tw, mc_s);
            }
 
            //=============================================================================================  

            WorkflowModulCapturaPrelucrareCaptura mc = new WorkflowModulCapturaPrelucrareCaptura();
 
            mc.M3_CopyResultonserver = Convert.ToByte(1);
            mc.M3_RevertSnapshot = Convert.ToByte(0);
 
            XmlSerializer xs = new XmlSerializer(typeof(WorkflowModulCapturaPrelucrareCaptura));
 
            string fileName_2 = "xmlTest_2.xml";
            using (TextWriter tw = new StreamWriter(fileName_2))
            {
                xs.Serialize(tw, mc);
            }
 
//====================================================================================
            RootElement root = new RootElement();
            root.ModulCapturaPrelucrareCaptura = mc;
            root.ModulSplitAnalizaSplit = mc_s;
            XmlSerializer xmlRoot = new XmlSerializer(typeof(RootElement));
 
            string fileName_3 = "xmlTest_3.xml";
            using (TextWriter tw = new StreamWriter(fileName_3))
            {
                xmlRoot.Serialize(tw, root);
            }
 
        }
 

 
File _1
 
<?xml version="1.0" encoding="utf-8"?>
<WorkflowModulSplitAnalizaSplit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <M0_RevertSnapshot>1</M0_RevertSnapshot>
  <M0_PowerOnVM>0</M0_PowerOnVM>
</WorkflowModulSplitAnalizaSplit>
 

File_2
 
<?xml version="1.0" encoding="utf-8"?>
<WorkflowModulCapturaPrelucrareCaptura xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <M3_CopyResultonserver>1</M3_CopyResultonserver>
  <M3_RevertSnapshot>0</M3_RevertSnapshot>
</WorkflowModulCapturaPrelucrareCaptura>
 

 

File_3
 
<?xml version="1.0" encoding="utf-8"?>
<RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ModulSplitAnalizaSplit>
    <M0_RevertSnapshot>1</M0_RevertSnapshot>
    <M0_PowerOnVM>0</M0_PowerOnVM>
  </ModulSplitAnalizaSplit>
  <ModulCapturaPrelucrareCaptura>
    <M3_CopyResultonserver>1</M3_CopyResultonserver>
    <M3_RevertSnapshot>0</M3_RevertSnapshot>
  </ModulCapturaPrelucrareCaptura>
</RootElement>
 

 
They all write to each files as expected.
 
Regards
Jegan
  Permalink  
v4
Comments
belea17 at 22-Feb-13 9:46am
   
Thanky you Jegan for your solution, but is the same. It writes in only one node...
Regards
Daniela
Jegan Thiyagesan at 22-Feb-13 9:53am
   
Can you post the xml file?
 
Can you also post the code behind the "WorkflowModulCapturaPrelucrareCaptura" and the "WorkflowModulSplitAnalizaSplit" ?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 Kornfeld Eliyahu Peter 407
2 Maciej Los 369
3 DamithSL 221
4 OriginalGriff 218
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100