C#篇之---XML

来源:互联网 发布:有个网络歌手老头唱歌 编辑:程序博客网 时间:2024/05/29 13:32

创建XML文档

例如要保存一个下面这样的XML文档:

<?xml version="1.0"?>
<Student>
  <Beck State="fine">
    <Id>20032187</Id>
    <Age>male</Age>
  </Beck>
  <Rose>
    <Id>20032188</Id>
    <Age>female</Age>
  </Rose>
</Student>

代码为:

            System.Xml.XmlDocument doc;
            doc = new System.Xml.XmlDocument();

            System.Xml.XmlNode node = doc.CreateNode(
                System.Xml.XmlNodeType.XmlDeclaration,
                "",
                "");
            doc.AppendChild(node);

            System.Xml.XmlNode student = doc.CreateNode(
                System.Xml.XmlNodeType.Element,
                "Student",
                "");
            doc.AppendChild(student);

            //beck node
            System.Xml.XmlNode beck = doc.CreateNode(
                System.Xml.XmlNodeType.Element,
                "Beck",
                "");
            student.AppendChild(beck);

            System.Xml.XmlElement beck_id = doc.CreateElement("", "Id", "");
            System.Xml.XmlText beck_id_value = doc.CreateTextNode("20032187");
            beck_id.AppendChild(beck_id_value);
            beck.AppendChild(beck_id);

            System.Xml.XmlElement beck_age = doc.CreateElement("", "Age", "");
            System.Xml.XmlText beck_age_value = doc.CreateTextNode("male");
            beck_age.AppendChild(beck_age_value);
            beck.AppendChild(beck_age);

            System.Xml.XmlAttribute beck_state = doc.CreateAttribute("State");
            beck_state.Value = "fine";
            beck.Attributes.Append(beck_state);

            //rose node
            System.Xml.XmlNode rose = doc.CreateNode(
               System.Xml.XmlNodeType.Element,
               "Rose",
               "");
            student.AppendChild(rose);

            System.Xml.XmlElement rose_id = doc.CreateElement("", "Id", "");
            System.Xml.XmlText rose_id_value = doc.CreateTextNode("20032188");
            rose_id.AppendChild(rose_id_value);
            rose.AppendChild(rose_id);

            System.Xml.XmlElement rose_age = doc.CreateElement("", "Age", "");
            System.Xml.XmlText rose_age_value = doc.CreateTextNode("female");
            rose_age.AppendChild(rose_age_value);
            rose.AppendChild(rose_age);

读取XML文档

例如如下XML文档:

<?xml version="1.0"?>
<Students>
  <Student id="20032187">
    <Sex>male</Sex>
    <Age>30</Age>
    <Name>
      <First>Beck</First>
      <Last>Yang</Last>
    </Name>
  </Student>
  <Student id="20032188">
    <Sex>female</Sex>
    <Age>30</Age>
    <Name>
      <First>Rose</First>
      <Last>Xi</Last>
    </Name>
  </Student>
  <Student id="20032186">
    <Sex>female</Sex>
    <Age>18</Age>
    <Name>
      <First>Yuting</First>
      <Last>Chi</Last>
    </Name>
  </Student>
</Students>

建立一个XML操作类:

class XmlOperator
    {
        private System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

        public bool LoadFile(string url)
        {
            doc.Load(url);
            return true;
        }

        public string GetNodeAtribute(string strAtt)
        {
            return null;
        }

        public string GetSimpleNodeValue(string ele, string key)
        {
            XmlNode root = doc.DocumentElement;
            XmlNode beck = root.SelectSingleNode(ele);
            string value = beck[key].InnerText;
            return value;
        }

        private XmlNode GetNodeMatchValue(string element, string key, string value)
        {
            XmlNode root = doc.DocumentElement;
           // XmlNode rose = root.SelectSingleNode("descendant::Student[Name/First='Rose']");
            string desp = "descendant::" + element + "[" + key + "='" + value + "']";
            XmlNode node = root.SelectSingleNode(desp);
            return node;
        }

        public string GetValueFromKey(string element, string key, string value, string keyDes)
        {
            XmlNode node = GetNodeMatchValue(element, key, value);
            if (node == null)
                return null;

            string result= node[keyDes].InnerText;
            return result;

        }

        public string GetAttrFromKey(string element, string key, string value, string attrDes)
        {
            XmlNode node = GetNodeMatchValue(element, key, value);
            if (node == null)
                return null;

            string result = node.Attributes[attrDes].Value;
            return result;

        }

        private XmlNode GetNodeMatchAttr(string element, string attr, string value)
        {
            XmlNode root = doc.DocumentElement;
            // XmlNode rose = root.SelectSingleNode("descendant::Student[Name/First='Rose']");
            string desp = "descendant::" + element + "[@" + attr + "='" + value + "']";
            XmlNode node = root.SelectSingleNode(desp);
            return node;
        }
        public string GetValueFromAttr(string element, string attr, string value, string keyDes)
        {
            XmlNode root = doc.DocumentElement;
            // XmlNode rose = root.SelectSingleNode("descendant::Student[Name/First='Rose']");
            string desp = "descendant::" + element + "[@" + attr + "='" + value + "']";
            XmlNode node = root.SelectSingleNode(desp);
            if (node == null)
                return null;

            string result = node[keyDes].InnerText;
            return result;

        }
        public string GetAttrFromAttr(string element, string attr, string value, string attrDes)
        {
            XmlNode root = doc.DocumentElement;
            // XmlNode rose = root.SelectSingleNode("descendant::Student[Name/First='Rose']");
            string desp = "descendant::" + element + "[@" + attr + "='" + value + "']";
            XmlNode node = root.SelectSingleNode(desp);
            if (node == null)
                return null;

            string result = node.Attributes[attrDes].Value;
            return result;

        }
    }

SelectSingleNode可以简单用SelectSingleNode("Student");

客户端调用如下(分别寻找匹配key-value的和匹配属性值得节点,然后获取element对应value或attribute):

//get node value from standard xml match key value
            //XmlOperator op = new XmlOperator();
            //op.LoadFile("standardStudent.xml");
            //string value = op.GetValueFromKey("Student", "Name/First", "Rose", "Age");
            //xmlValue.Text = value;

            //get attribute from standard xml match key value
            //XmlOperator op = new XmlOperator();
            //op.LoadFile("standardStudent.xml");
            //string value = op.GetAttrFromKey("Student", "Name/First", "Rose", "id");
            //xmlValue.Text = value;

            //get attribute from standard xml match attribute
            //XmlOperator op = new XmlOperator();
            //op.LoadFile("standardStudent.xml");
            //string value = op.GetAttrFromAttr("Student", "id", "20032187", "id");
            //xmlValue.Text = value;

            //get key value from standard xml match attribute
            XmlOperator op = new XmlOperator();
            op.LoadFile("standardStudent.xml");
            string value = op.GetValueFromAttr("Student", "id", "20032187", "Sex");
            xmlValue.Text = value;

 

 

 

0 0