2

I am rewriting an old .dll into net.core (1.1.1) and one of the methods creates an XmlDocument and defines the namespace via XmlNamespaceManager:

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

This throws the following error on build:

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

According to most posts I've seen, System.Xml.ReaderWriter is a NuGet, which I do not have installed. I've tried prefixing the XmlNamespaceManager with System.Xml but that made no difference.

EDIT:

This is the 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>

EDIT 2:

The Problem seems to be, that System.Xml.ReaderWriter is added whenever I add a reference to System.Xml. Setting an alias for System.Xml does not work and I cannot figure out how to set an alias for System.Xml.ReaderWriter.

4ndy
  • 550
  • 1
  • 9
  • 23
  • I don't know about .NET Core here, but if the compiler says it's finding the type in `System.Xml.ReaderWriter`, then you've _somehow_ included an assembly reference that includes that namespace and type. It's not possible to diagnose this without a reproducible scenario. You'll need to either provide a _lot_ more information, or take the time to look at your references and figure it out yourself. – Peter Duniho Apr 25 '17 at 23:57
  • Hi @PeterDuniho, I've added the csproj. These are the only references I've got – 4ndy Apr 26 '17 at 00:25
  • How did you add alias for system.xml ? Did you edit the projfile to add this tag after the hint path for System.xml youralias. After this did u change the code to add the external alias before the using statement and use the alias in the code. Don't say it doesn't work without telling what u did. – loneshark99 Apr 26 '17 at 10:45
  • Sorry, @loneshark99. The alias worked. It came up in the code as extern. But I still couldn't refer to the XMLDocument. Anyway, I solved it another way, without needing the XMLDocument in that class. I wrote another method that took a string and then assembled all the data at the time of sending the request I needed to send. Works fine. --- thank you for sending me to the links, though. I had never heard of aliases. – 4ndy Apr 27 '17 at 10:23

1 Answers1

0

The issue is since the class is present in both the dlls [Same (Namespace + TypeName) ] and its not able to resolve which one you want to use.

You should extern Alias the dll i.e alias the Dlls. In Visual Studio you can look at the dll properties and then give alias in the alias column . Then in the code refer to that dll using

extern alias dllaliasname;

This should get rid of the build error.

Take a look at this link.

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

Also take a look at this.

When must we use extern alias keyword in C#?

Community
  • 1
  • 1
loneshark99
  • 706
  • 5
  • 16
  • You might have missed some step because this is made for the scenario that you are mentioning . Anyways good luck – loneshark99 Apr 26 '17 at 00:54