none
xml custom header? RRS feed

  • Question

  • Dear All,

    Is it possible add custom header to XML file?

    Because data structure may be changed for different version.

    Hope to read the custom header to distinguish which version, then parse the data structure.

    Thanks and regards,

    E-John

    // version 1

    class Node {

    int Id;

    }

    // version 2

    class Node {

    int Id;

    int ParentId;

    }

    Tuesday, January 23, 2018 6:32 AM

All replies

  • Processing instructions can be used for something similar to custom headers. However, I don't think they are normally used for indicating the version of a data format. Instead, some XML file formats (at least XSLT and SVG) have a version attribute in the root element. That would be pretty simple to handle with XmlReader: skip to the root element, check the version, and then branch to code that deserializes the correct type.

    What do you use for translating the objects to XML and back: XmlSerializer, DataContractSerializer, System.Xml.Linq, or plain XmlWriter & XmlReader?

    Tuesday, January 23, 2018 6:23 PM
  • Hi Ranta,

    I would like to save my treeview with custom data to file and load it back when needed. What kind of serialization is undetermined.

    I use binary serialization to do save / load treeview curr

    There are namespace and many record tags in binary serialization file. This way is not proper for data compatible for different application. So I would like to serialize my treeview with custom data by xml serialization.

    Treeview with custom data structure will be changed if clients asks for new requirement. So I need to use version to distinguish different version data structure.

    I just found that there is a head body for xml structure, this maybe solve my issue. 

    We could parse the head then determine what kind of data structure should be apply to the deserialization function to deserialize data correctly. Thanks.

    Or there is any better suggestion and example?

    Thanks and regards,

    E-John

    www.xmlpatterns.com/HeadBodyMain.shtml

    <?xml version="1.0" encoding="utf-8"?>
    <CustomNodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Nh>
        <version>100</version>
        <model>Model-1</model>
      </Nh>
      <Nb>
        <NodeBody>
          <idx>0</idx>
          <pidx>0</pidx>
        </NodeBody>
        <NodeBody>
          <idx>1</idx>
          <pidx>0</pidx>
        </NodeBody>
      </Nb>
    </CustomNodes >

    Wednesday, January 24, 2018 4:22 AM
  • Hi E-John,

    Thank you for posting here.

    For your question, try to use XmlTextWriter to create a custom header.

    https://www.google.com.sg/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwj8p5fsk_DYAhWFwLwKHWPPARoQFggmMAA&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F25021095%2Fusing-xmlwriter-to-create-a-custom-header-in-c-sharp&usg=AOvVaw1lKbnov8wf-VZ7E3GwCga1

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 24, 2018 8:23 AM
    Moderator
  • I believe you go at this the wrong way. If you have data that is dynamic, you can just use the <data name="#nameofnode#">#valueofnode#</data> pattern. no need to go all fancy with versioning of XML and including headers and such.

    Anyways, if you want to use headers, you can create an object wich has the version attributes and a property for say "DataBase". Derived from that DataBase you can add the the real data. Problem is, you need to open each XML with the XML-Reader first to find out the version and map that version to the appropriate Derived DataBase. Thats why i say, use the name-value pattern instead.

    Problem with the name-value pattern is, your customers probably dont want that pattern but proper nodes, though they probably dont want any header in there neither but thats something else to figure out. Therefore, if you want to work with dynamic data in your application and your clients want static XML, you can use the XMLReader to convert the XML into name-value patterns and work with those under the hood. From my experience it is easier to write reusable code with such an approach than needing to use different formatted classes that carry the same information.

    BTW: XML does not have head and body parts, you can just create them if you need them, its XML after all, names are your decision or maybe your customers.

    If you do however decide to use a custom header (DOCTYPE node), beware that you might disable the possibility to deserialize and serialize the data with the XmlSerializer and maybe even other XML Tools. So your customers maybe wont be happy with that neither.


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.

    Wednesday, January 24, 2018 11:14 AM