xmlDoc.documentElement为null或不是对象

本文介绍了一种在使用JavaScript读取XML文档时遇到的乱码问题及解决方法。作者通过删除乱码内容成功解决了xmlDoc.documentElement为null的问题。
         今天在开发中遇到用js读取XML文档后进行操作时,出现xmlDoc.documentElement为null或不是对象的错误,网上找了很多说法和解决方法,一一试下来,都不能解决,后来打开XML文档,发现有中文乱码,于是考虑是否由于乱码而发生了错误,遂将乱码部分删去,再次试验,程序得以正常运行。
using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; namespace BMS { internal class Program { static void Main(string[] args) { //AddBook("006", "龙族", "江南"); //Console.WriteLine(SearchBook("斗破苍穹")); //Update("斗破苍穹", "00001"); } //遍历所有数据 public static void getAll() { //输出xml文件 文件中的数据 XmlDocument xmlDoc = new XmlDocument(); //加载xml文件 xmlDoc.Load("../../Books.xml"); //获取根节点、 XmlNode root = xmlDoc.DocumentElement; //遍历根节点下的所有子节点 foreach (XmlNode node in root.ChildNodes) { //遍历子节点的所有子节点 foreach (XmlNode childNode in node.ChildNodes) { Console.WriteLine(childNode.Name + "=" + childNode.InnerText); } Console.WriteLine("\n==============="); } } //查找图书 public static bool SearchBook(string bookname) { //输出xml文件 文件中的数据 XmlDocument xmlDoc = new XmlDocument(); //加载xml文件 xmlDoc.Load("../../Books.xml"); //获取根节点 XmlNode root = xmlDoc.DocumentElement; //遍历根节点下的所有子节点 foreach (XmlNode booknode in root.ChildNodes) { //遍历子节点的所有子节点 foreach (XmlNode childNode in booknode.ChildNodes) { if (childNode.Name.Equals("Bookname")) { if (childNode.InnerText.Equals(bookname)) { return true; } } } } return false; } //增加图书 public static void AddBook(string Bookid,string Bookname,string Bookauthor) { //创建xml 文档对象 XDocument doc = XDocument.Load("../../Books.xml"); //创建Books 元素 XElement bookElem = new XElement("Books", new XElement("Bookid", Bookid), new XElement("Bookname", Bookname), new XElement("Bookauthor", Bookauthor)); //添加到根节点下 doc.Root.Add(bookElem); //保存 doc.Save("../../Books.xml"); } //修改图书信息 public static void Update(string name, string id) { //创建xml 文档对象 XDocument doc = XDocument.Load("../../Books.xml"); //查找指定的学生节点 var Books = doc.Root.Elements("Book") .Where(s => (string)s.Element("Bookname") == name).FirstOrDefault(); //修改学生的id if (Books != null) { Books.Element("Bookid").Value = id; Console.WriteLine("修改成功"); } else { Console.WriteLine("未找到指定图书"); } //保存 doc.Save("../../Books.xml"); } //删除图书 public static void Delete(string Bookname) { //创建xml 文档对象 XDocument doc = XDocument.Load("../../Books.xml"); } } } 补全代码
11-09
/* * 由SharpDevelop创建。 * 用户: Administrator * 日期: 03/13/2014 * 时间: 21:41 * * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件 * */ using System; using System.Xml; using System.Collections; using demo_chechBox.baseMain; using System.Text; namespace demo_chechBox.tool { /*解析城市xml*/ public class AnalyticalXml { public static string citiesXmlName = "../../xml/comboBox/Cities.xml"; public static string districtsXmlName = "../../xml/comboBox/Districts.xml"; public static string userXmlName = "../../xml/dataSource/user.xml"; public AnalyticalXml() { } /*获取所有城市*/ public static IList findAllCity() { IList array = new ArrayList(); XmlDocument xmlDoc = getXmlDocument(citiesXmlName); XmlNodeList xnlList = xmlDoc.SelectNodes("Cities/City"); foreach(XmlNode x in xnlList) { array.Add(x.InnerText); } return array; } /**根据城市查询区*/ public static IList findAllDistricts(string city) { IList array = new ArrayList(); //获取城市ID XmlElement xe = getElementByCity(city); string cityId = xe.GetAttribute("ID"); //查询城市下边的子区 XmlDocument xmldoc = getXmlDocument(districtsXmlName); XmlNodeList nodeList = xmldoc.SelectNodes("Districts/District[@CID='"+cityId+"']"); foreach (XmlNode xn in nodeList) { array.Add(xn.InnerText); } return array; } /**以username为条件查询指定User*/ public static User getUser(string username) { XmlDocument xmldoc = getXmlDocument(userXmlName); XmlElement xe = (XmlElement)xmldoc.SelectNodes("users/user[@username='"+username+"']")[0]; if(xe!=null) { User user = new User(); user.setPassword(xe.GetAttribute("password")); user.setUsername(xe.GetAttribute("username")); user.setAddress(xe.GetAttribute("address")); user.setFileName(xe.GetAttribute("fileName")); user.setHobby(xe.GetAttribute("hobby")); user.setPhoto(xe.GetAttribute("photo")); user.setRemarks(xe.GetAttribute("remarks")); user.setBirtday(xe.GetAttribute("birtday")); user.setGender(xe.GetAttribute("gender")); return user; } return null; } public static XmlElement getElementByCity(string city) { XmlDocument xmlDoc = getXmlDocument(citiesXmlName); XmlNode xmlde = xmlDoc.SelectNodes("Cities/City[@CityName='"+city+"']")[0]; XmlElement xe = xmlde as XmlElement; return xe; } //保存user public static void saveUser(User user) { XmlDocument xmldoc = getXmlDocument(userXmlName); XmlNode root = xmldoc.DocumentElement; //创建新节点 XmlElement newChild = xmldoc.CreateElement("user"); newChild.SetAttribute("guid",user.Guid); newChild.SetAttribute("username",user.getUsername()); newChild.SetAttribute("password",user.getPassword()); newChild.SetAttribute("gender",user.getGender()); newChild.SetAttribute("birtday",user.getBirtday()); newChild.SetAttribute("hobby",user.getHobby()); newChild.SetAttribute("address",user.getAddress()); newChild.SetAttribute("photo",user.getPhoto()); newChild.SetAttribute("remarks",user.getRemarks()); newChild.SetAttribute("fileName",user.getFileName()); //添加users的最后一个子级节点后面 //XmlNode refChild = xmldoc.SelectNodes("users")[0]; XmlNode child = root.LastChild; if(child!=null) { root.InsertAfter(newChild,root.LastChild); } else { root.AppendChild(newChild); } //保存 XmlTextWriter xmlWriter = new XmlTextWriter(userXmlName,Encoding.GetEncoding("utf-8")); xmlWriter.Formatting = Formatting.Indented; xmldoc.Save(xmlWriter); xmlWriter.Close(); } public static XmlDocument getXmlDocument(string xmlName) { XmlDocument xmldoc = new XmlDocument(); try { xmldoc.Load(xmlName); } catch(Exception ex) { throw new OverflowException("找不到"+xmlName+"文件,抛出"+ex.Message); } return xmldoc; } } } 逐行解释代码并解释system是什么意思
09-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值