创建指定的xml文档二

指定XML格式为

 

<班级>

<学生 学号="0443111241" 姓名="张三">

<成绩 语文="88" 数学="99" 英语="100" />

</学生>

<学生 学号="0443111121" 姓名="李四">

<成绩 语文="92" 数学="90" 英语="96" />

</学生>

</班级>

 

        public string xmlCreate()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<班级></班级>");
            XmlNode newNode =doc.CreateNode("element","学生","");
            XmlNode root = doc.DocumentElement;
            root.AppendChild(newNode);
            XmlAttribute newAttribute = doc.CreateAttribute("学号");
            newAttribute.Value = "0443111241";
            newNode.Attributes.Append(newAttribute);
            newAttribute = doc.CreateAttribute("姓名");
            newAttribute.Value = "张三";
            newNode.Attributes.Append(newAttribute);

            XmlNode newNode1 = doc.CreateNode("element", "成绩", "");
            newNode.AppendChild(newNode1);
            newAttribute = doc.CreateAttribute("语文");
            newAttribute.Value = "88";
            newNode1.Attributes.Append(newAttribute);
            newAttribute = doc.CreateAttribute("数学");
            newAttribute.Value = "99";
            newNode1.Attributes.Append(newAttribute);
            newAttribute = doc.CreateAttribute("英语");
            newAttribute.Value = "100";
            newNode1.Attributes.Append(newAttribute);
           

            newNode = doc.CreateNode("element", "学生", "");
            root.AppendChild(newNode);
            newAttribute = doc.CreateAttribute("学号");
            newAttribute.Value = "0443111121";
            newNode.Attributes.Append(newAttribute);
            newAttribute = doc.CreateAttribute("姓名");
            newAttribute.Value = "李四";
            newNode.Attributes.Append(newAttribute);

            newNode1 = doc.CreateNode("element", "成绩", "");
            newNode.AppendChild(newNode1);
            newAttribute = doc.CreateAttribute("语文");
            newAttribute.Value = "92";
            newNode1.Attributes.Append(newAttribute);
            newAttribute = doc.CreateAttribute("数学");
            newAttribute.Value = "90";
            newNode1.Attributes.Append(newAttribute);
            newAttribute = doc.CreateAttribute("英语");
            newAttribute.Value = "96";
            newNode1.Attributes.Append(newAttribute);
            return doc.OuterXml;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值