Example #1
0
        public static void Obj2Xml()
        {
            //声明一个猫咪对象
            var cWhite = new Cat { Color = "White", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };
            var cBlack = new Cat { Color = "Black", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };

            CatCollection cc = new CatCollection { Cats = new Cat[] { cWhite, cBlack } };

            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));

            //将对象序列化输出到控制台
            serializer.Serialize(Console.Out, cc);
//<? xml version = "1.0" encoding = "gb2312" ?>
//   < cats xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema" >
//    < items >
//        < item color = "White" >
//            < saying > White or black, so long as the cat can catch mice,  it is a good cat</ saying >
//        </ item >
//        < item color = "Black" >
//            < saying > White or black, so long as the cat can catch mice,  it is a good cat</ saying >
//        </ item >
//    </ items >
//</ cats >


Console.ReadLine();
        }
Example #2
0
        public static void Obj2Xml()
        {
            //声明一个猫咪对象
            var cWhite = new Cat {
                Color = "White", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat"
            };
            var cBlack = new Cat {
                Color = "Black", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat"
            };

            CatCollection cc = new CatCollection {
                Cats = new Cat[] { cWhite, cBlack }
            };

            //https://stackoverflow.com/questions/2500111/how-do-i-add-a-default-namespace-with-no-prefix-using-xmlserializer
            //remove default namespace
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            //remove default namespace
            //namespaces.Add(String.Empty, string.Empty);
            //add cusotmer namespace without prefix, note, at the XmlSerializer, you should add the namespace value too..
            namespaces.Add(String.Empty, "www.test.com");

            //序列化这个对象
            //XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));

            //add customer namespace
            XmlSerializer serializer = new XmlSerializer(typeof(CatCollection), "www.test.com");

            //将对象序列化输出到控制台
            serializer.Serialize(Console.Out, cc, namespaces);

            //<? xml version = "1.0" encoding = "gb2312" ?>
            //   < cats xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema" >
            //    < items >
            //        < item color = "White" >
            //            < saying > White or black, so long as the cat can catch mice,  it is a good cat</ saying >
            //        </ item >
            //        < item color = "Black" >
            //            < saying > White or black, so long as the cat can catch mice,  it is a good cat</ saying >
            //        </ item >
            //    </ items >
            //</ cats >


            Console.ReadLine();
        }