[XML & Serialization] Final output is not well-formed (as desired).

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EARNEST
    New Member
    • Feb 2010
    • 128

    [XML & Serialization] Final output is not well-formed (as desired).

    Desired output (obtained using XmlNode and appending children):
    Code:
      <?xml version="1.0" ?> 
    - <Definitions>
    - <Definition>
      <Acronym>some acro</Acronym> 
      <Name>some name</Name> 
    - <Value>
      <A>12</A> 
      <B>55</B> 
      <Alpha>124</Alpha> 
      <Beta>21</Beta> 
      </Value>
      </Definition>
    - <Definition>
      <Acronym>some acro 2</Acronym> 
      <Name>some name 2</Name> 
    - <Value>
      <A>121</A> 
      <B>5551</B> 
      <Alpha>1224</Alpha> 
      <Beta>321</Beta> 
      </Value>
      </Definition>
      </Definitions>
    What I get with a new method to create and append:
    *) On creation (when file does not exist)
    <?xml version="1.0" encoding="utf-8" ?>
    - <Definitions xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
    <Acronym>bad acro</Acronym>
    <Name>bad name</Name>
    - <Value>
    <A>-522</A>
    <B>-21</B>
    <Alpha>-121</Alpha>
    <Beta>-12</Beta>
    </Value>
    </Definitions>
    *) On appending
    <?xml version="1.0" encoding="utf-8"?>
    <Definitions xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
    <Acronym>bad acro</Acronym>
    <Name>bad name</Name>
    <Value>
    <A>-522</A>
    <B>-21</B>
    <Alpha>-121</Alpha>
    <Beta>-12</Beta>
    </Value>
    </Definitions><?x ml version="1.0" encoding="utf-8"?>
    <Definitions xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
    <Acronym>bad acro 2</Acronym>
    <Name>bad name 2</Name>
    <Value>
    <A>41</A>
    <B>87</B>
    <Alpha>987</Alpha>
    <Beta>-87</Beta>
    </Value>
    </Definitions>
    And here is the code:
    Code:
            /// <summary>
            /// not working 
            /// </summary>
            /// <param name="definition"></param>
            public void SerializeToXML(Definition definition)
            {
                if (File.Exists("definitions.xml"))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Definition));
                    FileInfo fileInfo = new FileInfo("definitions.xml");
                    Stream inStream = fileInfo.OpenRead();
                    Definition tempDef = xmlSerializer.Deserialize(inStream) as Definition;
                    inStream.Close();
          
                    tempDef = definition;
    
                    StreamWriter sw = fileInfo.AppendText();
    
                    xmlSerializer.Serialize(sw, tempDef);
                    sw.Close();              
                }
                else
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Definition));
                    TextWriter textWriter = new StreamWriter("definitions.xml");
                    xmlSerializer.Serialize(textWriter, definition);
                    textWriter.Close();
                }
            }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Thanks for posting the new thread :)

    Why do you assign tempDef on line 12, do nothing with it, then reassign it to definition on line 15?

    You also don't seem to be calling AppendChild on the XmlNode object that represents your file, so it's not going to insert it into anything. It looks like it's just going to create an XML representation of the definition and add it to the end of the file, which looks like what you're seeing.

    The general idea, for when you want to append xml is...
    • Load XML
    • Get Root Node
    • Serialize target data
    • Find place where target data goes
    • Append the serialized data to the appropriate node
    • Rewrite the file with the updated XML


    It looks like you're missing the step where you append the data to a note and write out the updated XML.

    Comment

    • EARNEST
      New Member
      • Feb 2010
      • 128

      #3
      cheers mate. managed to solve it. appreciate your input

      Comment

      Working...