该程序通过xmltextreader读取xml文件的问题是什么,返回“无”。 [英] what's the problem with this program which reads xml file by xmltextreader return "None"

查看:169
本文介绍了该程序通过xmltextreader读取xml文件的问题是什么,返回“无”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的xml文件:

my xml file:

<PersonelInfo>
  <Person>
    <KeyID>15</KeyID>
    <PersonelCode>12</PersonelCode>
    <Company>sth</Company>
    <FirstName>m</FirstName>
    <LastName>it</LastName>
  </Person>
</PersonelInfo>





reding xml文件



reding xml file

using (StringReader stringReader = new StringReader("C:\\Users\\sth\\Desktop\\admin715.xml"))
using (XmlTextReader reader = new XmlTextReader(stringReader))
{
    // Parse the file and display each of the nodes.
    Class_PersonelInfo prsn = null;
    if (reader.IsStartElement())
    {
        while (reader.Read())
        {
            switch (reader.Name)
            {
                case "PersonelCode":
                    prsn.prsnlCod = reader.Value;
                    break;
                case "Company":
                    prsn.jobLocation = reader.Value;
                    break;
                case "FirstName":
                    prsn.frstName = reader.Value;
                    break;
                case "LastName":
                    prsn.lstName = reader.Value;
                    break;
             }
        }
   }
}





问题是读者是无



the problem is reader is "none"

推荐答案

它的愚蠢错误你的StringReaderClass Expects String所以解决方案就像这样你必须像这样替换你的StringReader线



Its silly Mistake your StringReaderClass Expects String so Solution would be like this you had to replace your StringReader line like this

StringReader stringReader = new StringReader(File.ReadAllText("C:\\Users\\sth\\Desktop\\admin715.xml"));







您必须将字符串内容传递给StringReaderClass,以便从您的文件中读取所有内容,然后将其传递给xmlTextReader Class it会给你正确的结果。




You had to pass your string content to StringReaderClass so read all content from your file and then pass it to xmlTextReader Class it will give you proper result.


一旦尝试这样,这是读取xmal值并将其分配给属性的简单方法



once try like this, this is simple way to read xmal values and assign it to properties

class readxml
   {
       static void Main(string[] args)
       {
           var doc=XElement.Load(@"F:\WebApplication1\ConsoleApplication1\XMLFile1.xml");
           var ms = doc.Elements("Person");
           var test = ms.Select(s => new PresonTest() { FirstName = s.Element("FirstName").Value, LastName = s.Element("LastName").Value, PersonalCode = s.Element("PersonelCode").Value, Company = s.Element("Company").Value });

       }
   }

   public class PresonTest
   {
       public string FirstName { get; set; }
       public string  LastName { get; set; }
       public string PersonalCode { get; set; }
       public string Company { get; set; }
   }


这是用于创建这样的阅读器来读取文件的正确模式:

This is the correct pattern used to create such a reader to read a file:
string fileName = //...
using (XmlReader reader = XmlReader.Create(fileName)) {
   //...
}



班级 XmlReader 是抽象的。具体实现将通过选择其中一个创建 工厂方法及其参数来选择。如果您好奇读者的运行时类型是什么,请使用 reader.GetType()



By顺便说一下,处理XML的手动和硬编码方式很糟糕。以 Data Contract 的形式使用序列化要好得多: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



要使用数据合约,您不必使用WCF本身。



-SA


The class XmlReader is abstract. The concrete implementation will be chosen by the choice of one of those Create factory methods and their parameters. If you are curious what is the runtime type of your reader, use reader.GetType().

By the way, your "manual" and hard-coded way of dealing with XML is bad. It's much better to use serialization in the form of Data Contract: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

To use Data Contracts, you don't have to use WCF itself.

—SA


这篇关于该程序通过xmltextreader读取xml文件的问题是什么,返回“无”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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