【问题标题】:C# - XmlNamespaceManager exists in both 'System.Xml.ReaderWriter' (...) and 'System.Xml (...)'C# - XmlNamespaceManager 存在于“System.Xml.ReaderWriter”(...)和“System.Xml(...)”中
【发布时间】:2017-09-23 04:17:04
【问题描述】:

我正在将一个旧的 .dll 重写为 net.core (1.1.1),其中一种方法创建一个 XmlDocument 并通过 XmlNamespaceManager 定义命名空间:

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

这会在构建时引发以下错误:

The type 'XmlNamespaceManager' exists in both 'System.Xml.ReaderWriter, Version=4.1.0.0, (...) and 'System.Xml, Version=4.0.0.0, ...

根据我看到的大多数帖子,System.Xml.ReaderWriter 是一个 NuGet,我没有安装它。我尝试在 XmlNamespaceManager 前加上 System.Xml 前缀,但这没有任何区别。

编辑:

这是csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputType>exe</OutputType>
  </PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
  <ApplicationIcon />
    <OutputTypeEx>exe</OutputTypeEx>
  <StartupObject />
</PropertyGroup>
<ItemGroup>
  <Reference Include="System.Xml">
    <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\System.Xml.dll</HintPath>
  </Reference>
</ItemGroup>
<ItemGroup>
  <Folder Include="Properties\" />
</ItemGroup>
</Project>

编辑 2:

问题似乎是,每当我添加对System.Xml 的引用时,都会添加System.Xml.ReaderWriter。为System.Xml 设置别名不起作用,我不知道如何为System.Xml.ReaderWriter 设置别名。

【问题讨论】:

  • 我在这里不了解 .NET Core,但如果编译器说它在 System.Xml.ReaderWriter 中找到了类型,那么您以某种方式包含了一个程序集引用包括该名称空间和类型。如果没有可重现的场景,就不可能诊断出这种情况。您需要提供很多更多信息,或者花时间查看您的参考资料并自己弄清楚。
  • 嗨@PeterDuniho,我已经添加了csproj。这些是我得到的唯一参考资料
  • 你是如何为 system.xml 添加别名的?您是否编辑了 projfile 以在 System.xml youralias 的提示路径之后添加此标记。在此之后,您是否更改了代码以在 using 语句之前添加外部别名并在代码中使用别名。不要不告诉你做了什么就说它不起作用。
  • 对不起,@loneshark99。别名起作用了。它作为外部代码出现在代码中。但我仍然无法引用 XMLDocument。无论如何,我用另一种方式解决了它,不需要那个类中的 XMLDocument。我写了另一个方法,它接受一个字符串,然后在发送我需要发送的请求时组装所有数据。工作正常。 --- 不过,感谢您将我发送到链接。我从来没有听说过别名。

标签: c# xml


【解决方案1】:

问题在于该类存在于两个 dll [Same (Namespace + TypeName)] 中,并且它无法解析您要使用的类。

您应该 extern Alias the dll,即为 Dlls 取别名。在 Visual Studio 中,您可以查看 dll 属性,然后在别名列中给出别名。然后在代码中使用

引用该 dll

外部别名 dllaliasname;

这应该可以消除构建错误。

看看这个链接。

https://blogs.msdn.microsoft.com/ansonh/2006/09/27/extern-alias-walkthrough/

也看看这个。

When must we use extern alias keyword in C#?

【讨论】:

  • 您可能错过了一些步骤,因为这是为您提到的场景而设计的。总之祝你好运
猜你喜欢
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多