0

I have an XML file which contains 1) a serialized object and 2) a serialized hashtable. Here is my pseudo xml file(data.xml):

   //PROPERTIES of an object called "Information"
   <?xml version="1.0" encoding="utf-8"?>
   <Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <CompileScript>false</CompileScript>
   <SaveScript>false</SaveScript>
   <ConvertPaths>false</ConvertPaths>
   <UseUninstaller>true</UseUninstaller>
   <UninstallSuccess>[NAME] was successfully removed from your computer.  </UninstallSuccess>
   <UninstallPrompt>Are you sure you want to completely remove [NAME] and all of its components?</UninstallPrompt>
   <AllowChangeStartMenuFolderName>false</AllowChangeStartMenuFolderName>
   //... (many others)
  //Here starts KEYS & VALUES of my hashtable
   <entry>
  <fileName> My file name 1</fileName>
  <instDir>My file location 1</instDir>
  </entry>
  <entry>
  <fileName> My file name 2</fileName>
  <instDir>My file location 2</instDir>
  </entry>
  //...and so on.

My goal is to deserialize half of the XML file (up to "AllowChangeStartMenuFolderName") back to "Information" object and the remaining part back to a hashtable. My concerns:

  1. How do I tell the program to deserialize the file up to "AllowChangeStartMenuFolderName" and then STOP?
  2. How do I tell the program to start deserializing at a certain point of an XML file(starting from the "entry" tag and down to the end of the file)?

Can somebody at least put me in the right direction, I haven't found any information on the internet. Thanks

EDIT

I have tried :

   XmlTextReader reader = new XmlTextReader("...Path...\\data.xml");
   //the last element before the keys and values of a hashtable start
   reader.ReadToDescendant("AllowChangeStartMenuFolderName");
   XmlSerializer serializer = new XmlSerializer(typeof(Classes.Information));
   //info is a new empty object which should be filled with values from the XML file
   this.info = (Classes.Information)serializer.Deserialize(reader.ReadSubtree());
            reader.Close();

Throws me an exception at the last line - "Error in XML Document (27,4)".

My original XML (the first one was pseude code):

 <?xml version="1.0" encoding="utf-8"?>
 <Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <CompileScript>false</CompileScript>
 <SaveScript>false</SaveScript>
 <ConvertPaths>false</ConvertPaths>
 <UseUninstaller>true</UseUninstaller>
 <UninstallSuccess>[NAME] was successfully removed from your computer.</UninstallSuccess>
 <UninstallPrompt>Are you sure you want to completely remove [NAME] and all of its components?</UninstallPrompt>
 <AllowChangeStartMenuFolderName>false</AllowChangeStartMenuFolderName>
 <StartMenuShortcut>true</StartMenuShortcut>
 <StartMenuUninstallIcon>true</StartMenuUninstallIcon>
 <TheRightProgramFilesDirectoryUserView>Program Files (x86)</TheRightProgramFilesDirectoryUserView>
 <RadioButtonRadioButton>false</RadioButtonRadioButton>
 <CheckBoxRadioButton>false</CheckBoxRadioButton>
 <ClassicButtonRadioButton>false</ClassicButtonRadioButton>
 <AllowChangeDirectoryCheckBox>true</AllowChangeDirectoryCheckBox>
 <ApplicationDirectory>C:\Program Files(x86)\ANNAX\My application</ApplicationDirectory>
 <ApplicationName>My application</ApplicationName>
 <ApplicationVersion>1.0</ApplicationVersion>
 <ApplicationPublisher>My company, Inc.</ApplicationPublisher>
 <ApplicationWebsite>http://wwww.mycompany.com</ApplicationWebsite>
 <SetupIcon>Icons\modern-install.ico</SetupIcon>
 <GermanCheckBox>false</GermanCheckBox>
 <EnglishCheckBox>true</EnglishCheckBox>
 <FrenchCheckBox>false</FrenchCheckBox>
 <ThirtyTwoBitRadioButton>true</ThirtyTwoBitRadioButton>
 <SixtyFourBitRadioButton>false</SixtyFourBitRadioButton>
 <entry>
   <fileName>file1</fileName>
   <instDir>instDir1</instDir>
 </entry>
 <entry>
  <fileName>file2</fileName>
  <instDir>instDir2</instDir>
</entry>
<entry>
  <fileName>file3</fileName>
  <instDir>instDir3</instDir>
</entry>
<entry>
  <fileName>file4</fileName>
  <instDir>instDir4</instDir>
</entry>
<entry>
  <fileName>file5</fileName>
  <instDir>instDir5</instDir>
</entry>
<entry>
  <fileName>file6</fileName>
  <instDir>instDir6</instDir>
</entry>
<entry>
  <fileName>file7</fileName>
  <instDir>instDir7</instDir>
</entry>
<entry>
  <fileName>file8</fileName>
  <instDir>instDir8</instDir>
</entry>
<entry>
  <fileName>file9</fileName>
  <instDir>instDIr9</instDir>
</entry>

  • http://stackoverflow.com/questions/369792/how-to-deserialize-only-part-of-an-xml-document-in-c-sharp – CodeCaster Oct 14 '16 at 08:34
  • This solution didn't work for me. –  Oct 14 '16 at 08:35
  • I have tried to apply the solution of your link but it threw and exception and due to lack of explanation I failed to understand it completely. That question was also the only thing I found that was close to my problem. Didn't know what to do next, therefore I am asking - if not give me an answer, to at least point me in the right direction –  Oct 14 '16 at 08:43
  • 1
    Did you read the comment of the answer as well? Seems pretty well explained to me. Also another easier solution would be to prepare your data by extracting interesting part in different files. – Ouarzy Oct 14 '16 at 08:50

0 Answers0