ReadElementString缺少指令或程序集引用

本文关键字:程序集 引用 指令 ReadElementString | 更新日期: 2023-09-27 18:29:51

在使用System.XML.XmlReader对象时,我似乎无法使用ReadElementString。我得到以下错误:

"System.Xml.XmlReader"不包含的定义"ReadElementString"且没有扩展方法"ReadElementString"接受"System.Xml.XmlReader"类型的第一个参数可能是已找到(您是否缺少using指令或程序集引用?)

这是我的代码:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
public void ReadXml (XmlReader reader)
{
    this.pKidsID = reader.GetAttribute("KidsID");
    reader.MoveToContent();
    reader.ReadStartElement();
    this.FirstName = reader.ReadElementString("FirstName");
    this.Name = reader.ReadElementString("Name");
    reader.Read();
    reader.ReadEndElement();
}

下面是我的xml文件的一个示例。你能告诉我如何使用Linq-to-XML来读取这个文件吗:

<?xml version="1.0" encoding="utf-8"?>
<Kids>
  <Child>
    <Name>kidone</Name>
    <FirstName>elmo</FirstName>
  </Child>
  <Child>
    <Name>kidtwo</Name>
    <FirstName>elmo</FirstName>
  </Child>
</Kids>

ReadElementString缺少指令或程序集引用

检查项目引用并添加对System.Xml.dll文件的引用。由于您已经有了using System.Xml;指令,我怀疑dll没有被引用。

从"解决方案资源管理器"菜单中,选择"引用",它将显示所有引用的文件。右键单击并选择"添加引用…",然后添加System.Xml.dll文件。或者,也可以从"项目"菜单中执行此操作。

除此之外,您发布的代码看起来不完整,因为它不包含在类中,但我想您没有共享完整的代码。