locked
XML XmlSerialiation to File Problem RRS feed

  • Question

  • User-1112204889 posted

    Hi all,

    I have 2 methods which could serialise an object to xml or object to file

            public static string ConvertObjectToXML(object myObject)
            {
                string retVal = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (StreamWriter writer = new StreamWriter(ms))
                    {
                        using (XmlWriter xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = false, Encoding = Encoding.UTF8 }))
                        {
                            XmlSerializer xs = new XmlSerializer(myObject.GetType());
                            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                            namespaces.Add(string.Empty, string.Empty);
                            xs.Serialize(xmlWriter, myObject, namespaces);
                        }
                        // Set memory stream to 0 *read from beginning*
                        ms.Position = 0;
                        retVal = new StreamReader(ms).ReadToEnd();
                    }
                }
                return retVal;
            }
    
            public static void ConvertObjectToFile(string filePath, object myObject)
            {
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = false, Encoding = Encoding.UTF8 }))
                    {
                        XmlSerializer xs = new XmlSerializer(myObject.GetType());
                        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                        namespaces.Add(string.Empty, string.Empty);
                        xs.Serialize(xmlWriter, myObject, namespaces);
                    }
                }
            }

    when i serialise object to xml this is the output

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfCustomer>
      <Customer>
        <ExtensionData />
        <account_type>5</account_type>
        <address_city />
        <address_country />
        <address_postcode />
        <address_street />
        <amount_last_payment>0</amount_last_payment>
        <bank_account_number />
        <bank_branch />
        <bank_name />
        <branch_id>1</branch_id>
        <code>RCTest</code>
        <communicate_via>0</communicate_via>
        <contact_firstname>Test</contact_firstname>
        <contact_lastname />
        <contact_title />
        <credit_card_expiry>0001-01-01T00:00:00</credit_card_expiry>
        <credit_card_name />
        <credit_card_number />
        <credit_limit>0</credit_limit>
        <date_joined>0001-01-01T00:00:00</date_joined>
        <date_last_payment>0001-01-01T00:00:00</date_last_payment>
        <deleted>false</deleted>
        <discount_code>0</discount_code>
        <email />
        <employee_id>0</employee_id>
        <father_id>0</father_id>
        <fax />
        <group_id>1</group_id>
        <gst_exempt>false</gst_exempt>
        <id>323</id>
        <internal_val>0</internal_val>
        <lookup_number>2010006</lookup_number>
        <market>true</market>
        <marketing_1>21 Apr 2011</marketing_1>
        <marketing_10 />
        <marketing_2>21 Apr 2011</marketing_2>
        <marketing_3 />
        <marketing_4 />
        <marketing_5 />
        <marketing_6 />
        <marketing_7 />
        <marketing_8 />
        <marketing_9 />
        <marketing_all_sales>437</marketing_all_sales>
        <marketing_amount>217</marketing_amount>
        <marketing_discount>20</marketing_discount>
        <marketing_last_discount>0001-01-01T00:00:00</marketing_last_discount>
        <no_interest>false</no_interest>
        <note />
        <phone_home />
        <phone_mobile />
        <phone_work />
        <price>1</price>
        <revolving_credit_minimumpayment>0</revolving_credit_minimumpayment>
        <stop_credit>false</stop_credit>
        <tc_id>0</tc_id>
        <term_code />
        <term_days>0</term_days>
        <trading_name />
        <website />
        <work_0>0</work_0>
        <work_1>0</work_1>
        <work_2>0</work_2>
        <work_3>0</work_3>
      </Customer>
    </ArrayOfCustomer>

    but when i serialise the object to file it looks like

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfCustomer>
      <Customer>
        <ExtensionData />
        <account_type>5</account_type>
        <address_city />
        <address_country />
        <address_postcode />
        <address_street />
        <amount_last_payment>0</amount_last_payment>
        <bank_account_number />
        <bank_branch />
        <bank_name />
        <branch_id>1</branch_id>
        <code>RCTest</code>
        <communicate_via>0</communicate_via>
        <contact_firstname>Test</contact_firstname>
        <contact_lastname />
        <contact_title />
        <credit_card_expiry>0001-01-01T00:00:00</credit_card_expiry>
        <credit_card_name />
        <credit_card_number />
        <credit_limit>0</credit_limit>
        <date_joined>0001-01-01T00:00:00</date_joined>
        <date_last_payment>0001-01-01T00:00:00</date_last_payment>
        <deleted>false</deleted>
        <discount_code>0</discount_code>
        <email />
        <employee_id>0</employee_id>
        <father_id>0</father_id>
        <fax />
        <group_id>1</group_id>
        <gst_exempt>false</gst_exempt>
        <id>323</id>
        <internal_val>0</internal_val>
        <lookup_number>2010006</lookup_number>
        <market>true</market>
        <marketing_1>21 Apr 2011</marketing_1>
        <marketing_10 />
        <marketing_2>21 Apr 2011</marketing_2>
        <marketing_3 />
        <marketing_4 />
        <marketing_5 />
        <marketing_6 />
        <marketing_7 />
        <marketing_8 />
        <marketing_9 />
        <marketing_all_sales>437</marketing_all_sales>
        <marketing_amount>217</marketing_amount>
        <marketing_discount>20</marketing_discount>
        <marketing_last_discount>0001-01-01T00:00:00</marketing_last_discount>
        <no_interest>false</no_interest>
        <note />
        <phone_home />
        <phone_mobile />
        <phone_work />
        <price>1</price>
        <revolving_credit_minimumpayment>0</revolving_credit_minimumpayment>
        <stop_credit>false</stop_credit>
        <tc_id>0</tc_id>
        <term_code />
        <term_days>0</term_days>
        <trading_name />
        <website />
        <work_0>0</work_0>
        <work_1>0</work_1>
        <work_2>0</work_2>
        <work_3>0</work_3>
      </Customer>
    </ArrayOf

    as you can see it didn't close the </ArrayOf tag, any one have any ideas?

    Monday, April 7, 2014 8:29 PM

Answers

  • User-1112204889 posted

    nevermind, figured the problem was with the response object, it was losing the characters when outputing, but was perfectly fine when saving

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2014 9:41 PM

All replies

  • User-1112204889 posted

    nevermind, figured the problem was with the response object, it was losing the characters when outputing, but was perfectly fine when saving

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2014 9:41 PM
  • User1777983149 posted

    Have use Dataset? yes

    All serlies and deserlise handle by dataset method use below code

    -----Serlise normal data to XML formate----

    DataSet ds = new DataSet();
    ds = xmlstring();
    ds.WriteXml(HttpContext.Current.Server.MapPath("\\MetaTagAll\\allpagesmeta.xml"), XmlWriteMode.IgnoreSchema);

    -----Deserlise XML to our formate
    DataSet listofpage = new DataSet();
    listofpage.ReadXml(HttpContext.Current.Server.MapPath("\\allpagesmeta.xml"));

    ---End

    Tuesday, April 8, 2014 8:56 AM