visual-studio 如何修复“缺少根元素”。在进行 Visual Studio (VS) 构建时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3772486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:28:39  来源:igfitidea点击:

How to fix "Root element is missing." when doing a Visual Studio (VS) Build?

visual-studio-2010visual-studiovisual-studio-2008visual-build-professional

提问by Gerhard Weiss

How to fix "Root element is missing." when doing a Visual Studio (VS) Build?

如何修复“缺少根元素”。在进行 Visual Studio (VS) 构建时?

Any idea what file I should look at in my solution?

知道我应该在我的解决方案中查看什么文件吗?

Actually, I am getting this error message inside of "Visual Build Pro" when using using the "Make VS 2008" command. This command works just fine when building other solutions (like about 20) and I am not really sure why mine is getting the error.

实际上,使用“Make VS 2008”命令时,我在“Visual Build Pro”中收到此错误消息。这个命令在构建其他解决方案时工作得很好(比如大约 20 个),我不太确定为什么我的会收到错误。

Any help would be very much appreciated. :)

任何帮助将不胜感激。:)

I am using VS 2008 and Visual Build Pro 6.7.

我使用的是 VS 2008 和 Visual Build Pro 6.7。

采纳答案by Oded

Make sure any XML file (or any file that would be interpreted as an XML file by visual studio) has a correct XML structure - that is, one root element (with any name, I have use rootElementin my example):

确保任何 XML 文件(或任何会被 Visual Studio 解释为 XML 文件的文件)具有正确的 XML 结构——即,一个根元素(具有任何名称,我rootElement在我的示例中使用过):

<?xml version="1.0"?> 
<rootElement>
 ...
</rootElement>

回答by Elliot

In my case it was the xxxxxxxxxxxx.vcxproj.user file that was causing the problem (it was blank) after a crash. I renamed it and the problem went away.

在我的情况下,是 xxxxxxxxxxxx.vcxproj.user 文件在崩溃后导致了问题(它是空白的)。我重命名了它,问题就消失了。

回答by stombeur

You will also get 'root element is missing' when the BOM strikes :). BOM = byte order mark. This is an extra character that gets added to the start of a file when it is saved with the wrong encoding.
This can happen sometimes in Visual Studio when working with XML files. You can either code something to remove it from all your files, or if you know which file it is you can force visual studio to save it with a specific encoding (utf-8 or ascii IIRC).

当 BOM 发生时,您还将收到“缺少根元素”的信息:)。BOM = 字节顺序标记。这是一个额外的字符,当文件以错误的编码保存时,它会被添加到文件的开头。
在 Visual Studio 中处理 XML 文件时有时会发生这种情况。您可以编写代码以将其从所有文件中删除,或者如果您知道它是哪个文件,您可以强制 Visual Studio 使用特定编码(utf-8 或 ascii IIRC)保存它。

If you open the file in an editor other than VS (try notepad++), you will see two funny characters before the <? xml declaration.

如果您在 VS 以外的编辑器中打开文件(尝试记事本++),您会在 <? 之前看到两个有趣的字符。xml 声明。

To fix this in VS, open the file in VS and then depending on the version of VS

要在 VS 中解决此问题,请在 VS 中打开文件,然后根据 VS 的版本

  • File > Advanced Save Options > choose an appropriate encoding
  • File > Save As > keep the filename, click the drop-down arrow on the right side of the save button to select an encoding
  • 文件 > 高级保存选项 > 选择合适的编码
  • 文件>另存为>保留文件名,点击保存按钮右侧的下拉箭头选择编码

回答by Malik Khalil

In my case.I was getting missing element errorpointing to NuGet.Configfile. At that time it was looking some thing like this

就我而言,我遇到了指向文件的缺失元素错误NuGet.Config。当时它看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <repositoryPath>Packages</repositoryPath>
</settings>

then I just added configurationtag that actually wraps entire xml. Now working fine for me

然后我只是添加configuration了实际包装整个 xml 的标签。现在对我来说工作正常

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <settings>
     <repositoryPath>Packages</repositoryPath>
  </settings>
</configuration>

回答by user3037124

This error is caused by corrupted proj file.

此错误是由损坏的 proj 文件引起的。

Visual Studio always has backup project file at specific folder.

Visual Studio 始终在特定文件夹中备份项目文件。

Please browse to:

请浏览至:

C:\Users\<Your user>\Documents\Visual Studio <Vs version>\Backup Files\<your project>

You should see 2 files like this:

你应该看到 2 个这样的文件:

Original-May-18-2018-1209PM.<your project>.csproj
Recovered-May-18-2018-1209PM.<your project>.csproj

You only need copy file:

您只需要复制文件:

Original-May-18-2018-1209PM.<your project>.csproj

And re-name as

并重命名为

<your project>.csproj 

and override at root project folder.

并覆盖根项目文件夹。

Problem is solved!

问题解决了!

回答by Alf Moh

In my case, when I opened the .csprojfile, it was empty so I went to my previous commit in git and copied the contents of that file and pasted it my current .csprojfile. After which I deleted the .csproj.userfile, reloaded my project, and everything started working again.

在我的情况下,当我打开.csproj文件时,它是空的,所以我转到我在 git 中的上一次提交并复制该文件的内容并将其粘贴到我当前的.csproj文件中。之后我删除了.csproj.user文件,重新加载了我的项目,一切又开始工作了。

回答by Shawn Hill

I had this issue running VS 2017, on build I was getting the error that the 'root element was missing'. What solved it for me was going to Tools > Nuget Package Manager > Package Manager Settings > General > Clear all Nuget Caches. After doing that I ran the build again and it was fixed.

我在运行 VS 2017 时遇到了这个问题,在构建时我收到了“缺少根元素”的错误。为我解决的是Tools > Nuget Package Manager > Package Manager Settings > General > Clear all Nuget Caches。这样做之后,我再次运行构建并修复了它。

回答by Shawn Hill

I got same error. showing error Microsoft.Data.Entity could not loaded root element missing. When i delete that file from C:\Windows\Microsoft.NET\Framework\v4.0.30319 and again open my solution my problem was solved. Everything woks fine

我有同样的错误。显示错误 Microsoft.Data.Entity 无法加载根元素丢失。当我从 C:\Windows\Microsoft.NET\Framework\v4.0.30319 中删除该文件并再次打开我的解决方案时,我的问题就解决了。一切正常

回答by Sam Porch

You can also search for the file. Navigate to your project directory with PowerShell and run Get-FileMissingRoot:

您也可以搜索该文件。使用 PowerShell 导航到您的项目目录并运行 Get-FileMissingRoot:

function Get-FileMissingRoot {

    dir -recurse |
        where {
            ($_ -is [IO.FileInfo]) -and 
            (@(".xml", ".config") -contains $_.extension) 
        } |
        foreach {
            $xml = New-Object Xml.XmlDocument;
            $filename = $_.FullName
            try {
                $xml.Load($filename)
            }
            catch {
                write ("File: " + $filename)
                write ($_.Exception.Message)
            }
        }
}

回答by pascal

In my case, the file C:\Users\xxx\AppData\Local\PreEmptive Solutions\Dotfuscator Professional Edition\4.0\dfusrprf.xmlwas full of NULL.

就我而言,该文件C:\Users\xxx\AppData\Local\PreEmptive Solutions\Dotfuscator Professional Edition\4.0\dfusrprf.xml充满了 NULL。

I deleted it; it was recreated on the first launch of Dotfuscator, and after that, normality was restored.

我删了它; 它是在 Dotfuscator 首次启动时重新创建的,之后又恢复正常。