我有如下的XML:
<?xml version="1.0"?>
<productfeed>
<product>
<name>x</name>
<etc>z</etc>
</product>
<product>
<name>x</name>
<etc>z</etc>
</product>
</productfeed>
我今天尝试使用LINQ和XElement从XML中检索数据。我设法加载文件,但是我无法访问productfeed
节点(它返回null)。有趣的是,我可以直接从根遍历product
个节点,而我读到的那个节点不应该像那样跳过。我是否错误地解释了某些内容,也许productfeed
节点已经在根目录中,但那么我将如何获得其纯内容?
XElement root = XElement.Load("...path..."); // works
XElement productFeed = root.Element("productfeed"); // returns null
IEnumerable<XElement> products = root.Elements("product"); // works
答案 0 :(得分:2)
正式地,您应该将其加载到XDocument中。那将有一个Route::get('background_images_on', 'SessionVarController@backgroundImgsOn');
Route::get('background_images_off', 'SessionVarController@backgroundImgsOff');
元素作为Root属性。
XElement允许您跳过此步骤,它可以同时充当Document和root。修复非常简单:
<productfeed>
XElement productFeed = root; //.Element("productfeed");
和XElement
确实解析了相同的内容,但XDocument有一个(更完整的)文档包装器,在处理XDocument
处理指令等时需要这样做。