C# 将xml字符串结构转换为XDocument对象时出现的问题

C# 将xml字符串结构转换为XDocument对象时出现的问题,c#,asp.net-mvc,C#,Asp.net Mvc,我有要转换为XDocument对象的xml字符串。我一直在遵循微软的这个例子 问题不是像示例中那样得到下面的结果 <!-- comment at the root level --> <Root> <Child>Content</Child> </Root> 来自MSDN的给定示例提供了 <!-- comment at the root level --> <Root>

我有要转换为XDocument对象的xml字符串。我一直在遵循微软的这个例子

问题不是像示例中那样得到下面的结果

    <!-- comment at the root level -->  
<Root>  
  <Child>Content</Child>  
</Root>

来自MSDN的给定示例提供了

    <!-- comment at the root level -->  
<Root>  
  <Child>Content</Child>  
</Root>

内容
您发布的输出看起来像
XDocument
的所有属性。
XDocument
对象包含的信息不仅仅是您解析的普通XML。 在本例中,
Console.WriteLine(doc)行生成的输出
是您作为XML提供的字符串,因为它调用
doc.ToString()
,从而生成“原始”XML输出

因此,我认为您可能会对包含更多信息(属性比原始xml多)的
XDocument
感到困惑。但是,您可以使用LinqToXML()完美地查询XML数据


看起来解析工作完全正常(将原始XML解析为XDocument类型的对象)。

您能提供您正在使用的代码吗?下面是xmlString声明var xmlString=@“Content”;这就是我创建XDocument对象XDocument xDoc=XDocument.Parse(xmlString)的方法;MSDN示例按预期工作:。如果不发布任何代码,我们无法判断可能出现的错误。我已通过添加代码修改了我的问题。非常感谢。
<!-- comment at the root level -->  
    <Root>  
      <Child>Content</Child>  
    </Root>
var xmlString = @"<?xml version=""1.0""?><!-- comment at the root level --><Root><Child>Content</Child></Root>";
XDocument xDoc = XDocument.Parse(xmlString);
    <!-- comment at the root level -->  
<Root>  
  <Child>Content</Child>  
</Root>