【发布时间】:2015-05-28 19:21:03
【问题描述】:
我有一个看起来像这样的 XML 文件
<DatabaseInfo>
<DatabaseInformation>
<name>\\server\path\HelpDesk.accdb</name>
</DatabaseInformation>
</DatabaseInfo>
<ShortcutPath>
<ShortcutPathInformation>
<name>Y:\Shortcuts</name>
</ShortcutPathInformation>
</ShortcutPath>
我的 C# 代码看起来像
var result = (from ele in XDocument.Load(@"C:\Srptupd\Database.xml").Descendants("DatabaseInformation")
select ele).FirstOrDefault();
if (result != null)
{
//
}
我得到一个例外说
There are multiple root elements. Line 6, position 2.
如何获取 DatabaseInformation 和 ShortcutPathInformation 的名称值?
【问题讨论】:
-
如果上面的数据是你所有的数据,那么它不是有效的 XML。 XML 必须只有一个根元素。
-
XML 有两个根元素——
DatabaseInfo和ShortcutPath。 XML 文档必须有one single root。在这里尝试阅读具有多个片段的文档:stackoverflow.com/questions/2374426/…
标签: c# xml linq linq-to-xml