c# Xml

本次试验主要认识C#中操作Xml的两种方法:XDocument 和XmlDocument(还有第三种XmlReader,不喜欢这种,就不先去研究了)。再者在初窥序列化和反射。

0. 准备Xml文件

xml:

<?xml version="1.0" encoding="utf-8"?>
<Bookstore>
    <!--记录书本的信息-->
    <Book Type="必修课" ISBN="7-111-19149-2">
        <Title>数据结构</Title>
        <Author>严蔚敏</Author>
        <Price>30.00</Price>
    </Book>
        <Book Type="必修课" ISBN="7-111-19149-3">
        <Title>路由型与交换型互联网基础</Title>
        <Author>程庆梅</Author>
        <Price>27.00</Price>
    </Book>
</Bookstore>


1. 用XDocument来解析

    该方法支持LINQ。

private static void UseXDocument(string xmlpath)
        {
            XDocument xdoc = XDocument.Load(xmlpath);

<span style="white-space:pre">	</span>    // Just declare, do not do query
            var query = from e in xdoc.Descendants("Book")
                        select new Book()
                            {
                                Type = e.Attribute("Type").Value,
                                ISBN = e.Attribute("ISBN").Value,
                                Title = e.Element("Title").Value,
                                Author = e.Element("Author").Value,
                                Price = e.Element("Price").Value.ParseToDouble(),
                            };
            PrintBook(query);<span style="white-space:pre">	</span>// Do the query here
            Serializer.Serialize(query.First(), binpath);

        }

最后一句序列化对象存储为bin文件,见本文附录A。


2. 用XmlDocument来解析
<span style="white-space:pre">	</span>private static void UseXmlDocument(string xmlpath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlpath);

            foreach (XmlNode node in doc.SelectNodes("Bookstore//Book"))
            {
                Book book = new Book();

                // Set Field With Reflection
                foreach (XmlAttribute attr in node.Attributes)
                {
                    Reflector.SetField(book, attr.LocalName, attr.Value);
                }

                foreach (XmlNode child in node.ChildNodes)
                {
                    Reflector.SetField(book, child.Name, child.InnerText);
                }

                PrintBook(book);
            }
        }

在设置对象字段时,应用了反射,见附录B


附录 A 序列化和反序列化

    public static class Serializer
    {
        public static void Serialize(object obj, string binpath)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(binpath, FileMode.Create,
                                    FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, obj);
            stream.Close();
        }

        public static T Deserialize<T>(string binpath) where T : class
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(binpath, FileMode.Open,
                                    FileAccess.Read, FileShare.Read);
            T target = formatter.Deserialize(stream) as T;
            stream.Close();

            return target;
        }
    }



附录 B 反射


    public static class Reflector
    {
        public static void SetField(object obj, string field, string value)
        {
            try
            {
                FieldInfo fi = obj.GetType().GetField(field);


                var fieldTypeName = fi.FieldType.Name;


                // Parser Method Format: ParseTo + typename
                // e.g.: ParseToUint32
                MethodInfo mi = typeof(StringParser).GetMethod("ParseTo" + fieldTypeName);


                var v = mi.Invoke(null, new object[] { value });


                fi.SetValue(obj, v);
            }
            catch
            { }
        }
    }



附录 C String解析


    public static class StringParser
    {
        public static string ParseToString(this string str)
        {
            return str;
        }

        public static int ParseToInt32(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                return default(int);
            }
        }

        public static uint ParseToUInt32(this string str)
        {
            try
            {
                if (str.Contains("0x") || str.Contains("0X"))
                {
                    return Convert.ToUInt32(str, 16);
                }

                return Convert.ToUInt32(str);
            }
            catch
            {
                return default(uint);
            }

        }

        public static double ParseToDouble(this string str)
        {
            try
            {
                return Convert.ToDouble(str);
            }
            catch
            {
                return default(double);
            }
        }

        public static bool ParseToBoolean(this string str)
        {
            try
            {
                if (str == "0")
                {
                    return false;
                }

                if (str == "1")
                {
                    return true;
                }

                return Convert.ToBoolean(str);

            }
            catch
            {
                return default(bool);
            }
        }
    }









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值