黑马程序员技术交流社区
标题:
未将对象引用设置到对象的实例是怎么回事?c# xml 操作
[打印本页]
作者:
朱勋
时间:
2011-10-21 11:44
标题:
未将对象引用设置到对象的实例是怎么回事?c# xml 操作
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace ParseXML
{
/// <summary>
/// XML文件操作类
/// </summary>
class XMLFunc
{
/// <summary>
/// 创建XML文档
/// </summary>
/// <param name="path"></param>
public static void CreateXML(string path, Student stu)
{
XmlTextWriter xw = new XmlTextWriter(path, Encoding.UTF8);
xw.WriteStartDocument();
xw.Formatting = Formatting.Indented;
xw.WriteStartElement("Students");
xw.WriteStartElement("Student");
xw.WriteStartElement("SID");
xw.WriteStartAttribute("id");
xw.WriteValue(stu.SID.ToString());
xw.WriteEndAttribute();
xw.WriteValue(stu.SID.ToString());
xw.WriteEndElement();
xw.WriteStartElement("Name");
xw.WriteValue(stu.Name);
xw.WriteEndElement();
xw.WriteStartElement("Age");
xw.WriteValue(stu.Age.ToString());
xw.WriteEndElement();
xw.WriteStartElement("Sex");
xw.WriteValue(stu.Sex);
xw.WriteEndElement();
xw.WriteStartElement("Address");
xw.WriteValue(stu.Address);
xw.WriteStartElement("Telphone");
xw.WriteValue(stu.phone);
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndElement();
xw.Flush();
xw.Close();
MessageBox.Show(path + " 创建成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 追加
/// </summary>
/// <param name="path">XML文件路径</param>
/// <param name="stu">学员</param>
public static void Append(string path, Student stu)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNode xnl = xmlDoc.DocumentElement;
for (int j = 0; j < xnl.ChildNodes.Count; j++)
{
if (xnl.ChildNodes[j].ChildNodes[0].Attributes["id"].Value == stu.SID.ToString())
{
MessageBox.Show("已经包含了一个学号为:" + stu.SID + " 的学员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
XmlNode node = xmlDoc.SelectSingleNode("Students");
XmlElement xes = xmlDoc.CreateElement("Student");
XmlElement xeid = xmlDoc.CreateElement("SID");
xeid.SetAttribute("id", stu.SID.ToString());
xeid.InnerText = stu.SID.ToString();
XmlElement xename = xmlDoc.CreateElement("Name");//添加各个节点 name,age,sex,address,telphone
xename.InnerText = stu.Name;
XmlElement xeage = xmlDoc.CreateElement("Age");
xeage.InnerText = stu.Age.ToString();
XmlElement xesex = xmlDoc.CreateElement("Sex");
xesex.InnerText = stu.Sex;
XmlElement xeadd = xmlDoc.CreateElement("Address");
xeadd.InnerText = stu.Address;
XmlElement xeapp = xmlDoc.CreateElement("telphone");
xeadd.InnerText = stu.phone;
xes.AppendChild(xeid);
xes.AppendChild(xename);
xes.AppendChild(xeage);
xes.AppendChild(xesex);
xes.AppendChild(xeadd);
xes.AppendChild(xeapp);
node.AppendChild(xes);
xmlDoc.Save(path);
MessageBox.Show("追加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 修改
/// </summary>
/// <param name="path">XML文件路径</param>
/// <param name="id">学号</param>
/// <param name="stu">学员对象</param>
public static void Edit(string path, string id, Student stu)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNode xnl = xmlDoc.DocumentElement;
XmlNode node = null;
for (int j = 0; j < xnl.ChildNodes.Count; j++)
{
if (xnl.ChildNodes[j].ChildNodes[0].Attributes["id"].Value == id)
{
node = xnl.ChildNodes[j];
}
}
if (node == null)
return;
node.ChildNodes[1].InnerText = stu.Name;
node.ChildNodes[2].InnerText = stu.Age.ToString();
node.ChildNodes[3].InnerText = stu.Sex;
node.ChildNodes[4].InnerText = stu.Address;
node.ChildNodes[5].InnerText = stu.phone;
xmlDoc.Save(path);
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 删除操作
/// </summary>
/// <param name="path">XML路径</param>
/// <param name="id">删除的ID</param>
public static void Delete(string path, string id)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNode xnl = xmlDoc.DocumentElement;
XmlNode node = null;
for (int j = 0; j < xnl.ChildNodes.Count; j++)
{
if (xnl.ChildNodes[j].ChildNodes[0].Attributes["id"].Value == id)
{
node = xnl.ChildNodes[j];
}
}
if (node == null)
return;
node.ParentNode.RemoveChild(node);
xmlDoc.Save(path);
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 加载信息
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static IList<Student> LoadXML(string path)
{
IList<Student> stuList = new List<Student>();//创建一个Student类的集合
XmlDocument xmlDoc = new XmlDocument();//创建一个XmlDocument类的实例
xmlDoc.Load(path);
XmlNode node = xmlDoc.DocumentElement;
for (int i = 0; i < node.ChildNodes.Count; i++)
{
XmlNode xnl = node.ChildNodes[i];
Student stu = new Student();
stu.SID = xnl["SID"].InnerText;
stu.Name = xnl["Name"].InnerText;
stu.Age = Convert.ToInt32(xnl["Age"].InnerText);
stu.Sex = xnl["Sex"].InnerText;
stu.Address = xnl["Address"].InnerText;
stu.phone = xnl["Telphone"].InnerText;
stuList.Add(stu);
}
return stuList;
}
}
}
出现stu.phone = xnl["Telphone"].InnerText; 未将对象引用设置到对象的实例是怎么回事?谢谢大家
作者:
朱勋
时间:
2011-10-21 21:38
谢谢大家问题应经找到。{:soso_e113:}
作者:
朱勋
时间:
2011-10-22 11:38
xml生成是出现错误了
xw.WriteStartElement("Address");
xw.WriteValue(stu.Address);
xw.WriteStartElement("Telphone");
xw.WriteValue(stu.phone);
xw.WriteEndElement();
作者:
章坚
时间:
2011-10-24 13:50
嘿嘿 XML 才学习 是容易出些问题
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2