如何清除xmlnodes中的错误. [英] How to clear error in xmlnodes.

查看:67
本文介绍了如何清除xmlnodes中的错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,


我正在使用.net应用程序,并且有一个xml文档.我想获取childnodes的值并将其放入记事本中.运行程序后,发生错误.
根级别的数据无效.第1行,位置1.".请帮我.下面是代码

//Default.aspx.cs//

Hai,


I am having .net application and I have an xml document.I want to get the values of childnodes and put it into notepad.After I run the program the error is occurs.
"Data at the root level is invalid. Line 1, position 1.".Plz help me.Below is the code

//Default.aspx.cs//

XmlDocument xml = new XmlDocument();
       xml.LoadXml("C:/Documents and Settings/sridharan/Desktop/RTSLINK FOR TALLY/XMLFile3.xml");
       XmlNodeList xnlist = xml.SelectNodes("/Names/Name");
       foreach (XmlNode xn in xnlist)
       {
           string firstname = xn["firstname"].InnerText;
           string lastname = xn["secondname"].InnerText;
           Console.WriteLine("Name:{0} {1}", firstname, lastname);



//xmlfile//



//xmlfile//

<names>
  <name>
    <firstname>John</firstname>
    <lastname>Smith</lastname>
  </name>
  <name>
    <firstname>James</firstname>
    <lastname>White</lastname>
  </name>
</names>

推荐答案

讨论了类似的问题 [ ^ ]在此处详细说明.

可能的原因很少:
1.格式不正确的XML
2.在根元素之前留一个空格
3.文件引用错误
4. XML文档中指定的编码

看看此讨论 [ ^ ].

更新:
除了上述答案外,请尝试两件事:
1.使用xml.Load代替xml.LoadXml方法.
2.如果1无效,则将其作为第一行(确保第一个字符之前没有空格)
Similar issue discussed[^] here at length.

Few possible reasons are:
1. Not a well formed XML
2. Having a white space just before the root element
3. Wrong reference of the file
4. Encoding specified in XML document

Have a look at this discussion[^] too.

UPDATE:
Apart from above answer, try two things:
1. Use xml.Load instead of xml.LoadXml method.
2. If 1 does not do, put this as the first line (make sure no space before the first character)
<?xml version="1.0" encoding="utf-8"?>


这里是替代方法.我将完全避免这种方法.我不明白为什么需要手动处理XML.使用数据合同.它将健壮地将您的数据模型保存为XML并将其还原;模式将被动态创建.这也是最不介入的方法:您只添加属性[DataContract][DataMembers].

参见:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datamemberattribute.aspx [ ^ ].

—SA
Here is the alternative. I would totally avoid this approach. I don''t see why you need to deal with XML manually. Use Data Contract. It will robustly save your data model to XML and restore it back; the schema will be created on the fly. This is the most non-intrusive method as well: you only add attributes [DataContract] and [DataMembers].

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^].

—SA


这是您的问题所在:

XmlDocument具有2种用于加载XML的加载方法

Here''s what your problem is:

XmlDocument has 2 load methods for loading XML

XmlDocument xml = new XmlDocument();
xml.LoadXml(...)



此处传递的参数应该是有效的xml字符串.

您想要的方法是



the passed paramater here should be a valid xml string.

the method you want is

XmlDocument xml = new XmlDocument();
xml.Load("PathGoesHere.xml")



另外,XPath区分大小写(名称与名称不同)



Also, the XPath is case sensitive (Names is different than name)


这篇关于如何清除xmlnodes中的错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆