none
XML Deserialize / Serialize with the same namespace RRS feed

  • Question

  • Hello,
    I have an XML file, I copy it and paste it via the clipboard. Classes are created.
    I can read the values -> IO
    If I change values and save them again the namespaces are lost or look different. So how can I save it correctly? With same namespace as the original?
    Thanks in advance for tips.
    Greetings Markus
    <?xml version="1.0"?>
    <Msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Epicor.com/Message/2.0">
      <Hdr>
        <Ctrl>
          <MsgID />
        </Ctrl>
        <Sender>
          <Name />
          <Subname />
        </Sender>
    	
    // ##################################################################################

    static T Deserialize<T>(Stream stream)
    {
    	var serializer = new XmlSerializer(typeof(T));
    	using (stream)
    		return (T)serializer.Deserialize(stream);
    }
    
    using (var file = File.OpenRead(@"C:\Users\Freitag\Documents\TEST_M.xml"))
    {
       var gpx = Deserialize<Msg>(file);
    
    	var jobNo = gpx.Body.Req.Dta.Results.QueryResultDataSet.Results.JobHead_JobNum;
    
    	jobNo = 6666666;
    	gpx.Body.Req.Dta.Results.QueryResultDataSet.Results.JobHead_JobNum = 66663333;
    
    	 var serializer = new XmlSerializer(typeof(Msg));
    
    	using (var fileWrite = File.Create(@"C:\Users\Freitag\Documents\TEST_M_NEW.xml"))
    		serializer.Serialize(fileWrite, gpx);
    }
    <?xml version="1.0" encoding="utf-16"?>
    <msg:Msg xsi:schemaLocation="http://EBMThirdProducer.com/Message/2.0 http://scshost/schemas/epicor/ScalaMessage.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://EBMThirdProducer.com/Message/2.0">
      <msg:Hdr>
        <msg:Ctrl>
          <msg:MsgID></msg:MsgID>
        </msg:Ctrl>
        <msg:Sender>
          <msg:Name></msg:Name>
          <msg:Subname></msg:Subname>
        </msg:Sender>
        <msg:Receiver>
          <msg:Name>Company</msg:Name>
          <msg:Subname>OrderData</msg:Subname>
        </msg:Receiver>
        <msg:Logon></msg:Logon>
      </msg:Hdr>
      <msg:Body>
        <msg:Req msg-type="Company" action="GetOrderData">
          <msg:Dta>
            <ext_UserSchema:Results xmlns:msg="http://EBMThirdProducer.com/InternalMessage/1.1" xmlns:ext_UserSchema="http://EBMThirdProducer.com/SC/UserSchema">
              <QueryResultDataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <Results>
                  <JobHead_JobNum>116666580</JobHead_JobNum>
                  <JobHead_PartNum>555724307910003</JobHead_PartNum>
                  <JobHead_ProdQty>5.06000000</JobHead_ProdQty>
                  <CustXPrt_XPartNum>33666-355-03</CustXPrt_XPartNum>
                  <CustXPrt_XRevisionNum>D1</CustXPrt_XRevisionNum>
                  <CustXPrt_SNLastUsedSeq/>
                  <Calculated_TemplateID>31166-355-03</Calculated_TemplateID>
                </Results>
                <ExecutionInfo>
                  <Name>ExecutionTime</Name>
                  <Value>77.7252</Value>
                </ExecutionInfo>
              </QueryResultDataSet>
            </ext_UserSchema:Results>
          </msg:Dta>
        </msg:Req>
      </msg:Body>
    </msg:Msg>


    Monday, December 16, 2019 5:58 PM

All replies

  • Hi Markus,

    Thank you for posting here.

    I tested it with the code and XML file you gave, but everything works fine. Could you please provide more information so that I can reproduce your problem?

    And if you just want to add or modify the value of the element in the XMLfile, I suggest that you can consider using XmlDocument or XElement.


    Best Regards,
    Timon


    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.

    Tuesday, December 17, 2019 2:31 AM
  • Hello Timon,

    <Hdr> must be <msg:Hdr>

    and so one.

    Thanks for your help.

    Best regards Markus


    Tuesday, December 17, 2019 7:03 AM
  • The namespace in the original file is lost when you deserialize, since there is nothing in your class to store it. But you can declare it with an attribute, and it will be written into the file when you serialize:

    [XmlRoot("Msg", Namespace="http://EBMThirdProducer.com/Message/2.0")]
    public class Msg
    {
    }
    

    You can similarly decorate with attributes any members of the class that require a specific namespace.

    Tuesday, December 17, 2019 7:16 AM
    Moderator
  • Hello,
    From Clipboard
    namespace WpfAppTest
    {
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://EBMThirdProducer/Message/2.0")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://EBMThirdProducer/Message/2.0", IsNullable = false)]
        public partial class Msg
        {
    
            private MsgHdr hdrField;
    
            private MsgBody bodyField;
    
            /// <remarks/>
            public MsgHdr Hdr
            {
                get
                {
                    return this.hdrField;
                }
                set
                {
                    this.hdrField = value;
                }
            }
    
            /// <remarks/>
            public MsgBody Body
            {
    		
    	//------------------------
    I replace manually
    namespace WpfAppTest
    {
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://EBMThirdProducer/Message/2.0")]
        [System.Xml.Serialization.XmlRoot("Msg", Namespace = "http://EBMThirdProducer/Message/2.0", IsNullable = false)]
        public partial class Msg
        {
    
            private MsgHdr hdrField;
    
       // -----------
     Nothing happen.
     <?xml version="1.0"?>
    <Msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Epicor.com/Message/2.0">
      <Hdr>
        <Ctrl>
          <MsgID />
        </Ctrl>
    	
     
    Any other idea?
    Best regards Markus



    Tuesday, December 17, 2019 5:58 PM
  • Hi Markus,

    Thanks for your feedback.

    You can use XmlDocument to manage XML namespaces.

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(@"D:\test\xml\1213_test1.xml");
    
                //Get NameSpace.
                XmlAttributeCollection xmlAttributeCollection = xmlDocument.DocumentElement.Attributes;
    
                //Set NameSpace
                xmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://EBMThirdProducer/Message/2.0");
                
                xmlDocument.Save(@"D:\test\xml\1213_test1.xml");
                

    Hope this could be helpful.

    Best Regards,

    Timon


    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, December 18, 2019 2:58 AM
  • Hello Timon,

    I'm looking for a way to just solve it.

    So I have to create everything manually.

    About the property Flags [] it would be easy .

    Maybe not possible. Can you please answer yes or no? Or someone who knows.

       Thanks in advance.

    Best regards Markus


    Tuesday, December 24, 2019 4:07 PM
  • Hi Markus,

    I have no good ideas to solve it now. If I have a new idea, I will inform you immediately.

    Best Regards,

    Timon


    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, December 25, 2019 1:19 AM
  • I have no good ideas to solve it now. If I have a new idea, I will inform you immediately.


    Hello Timon,

    You know what I mean? Should I close the thread?

    With best regards  Markus

    Monday, December 30, 2019 4:23 PM
  • Hi Markus,

    In the past few days, I have tried to find a good solution to this problem.

    But unfortunately I didn't find it.

    I'm sorry my replies didn't help you, but these replies may provide some ideas for people who have other questions to see this thread. So you don't need to close it, just put it here.

    Best Regards,

    Timon


    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.

    Tuesday, January 7, 2020 9:50 AM