如何在 WebMatrix razor (C#) 中使用 XML [英] How to use XML with WebMatrix razor (C#)

查看:79
本文介绍了如何在 WebMatrix razor (C#) 中使用 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何使用(特别是阅读)XML 与我使用的至少一种语言(最好是 WebMatrix C#),但我过去曾多次尝试使用 JavaScript 来做到这一点,但是没有任何在线示例(在 StackOverflow 或其他方式中)足够完整以使其对我有用(请记住,我没有实际的 XML 经验,我当然已经完成了有关 XPath、XML 等的简单教程,但我们都知道这些教程是多么的简短和不完整).

I need to know how to use (specifically just read) XML with at least one of the languages I use (preferably this would be WebMatrix C#), but I have tried to do this with JavaScript several times in the past, but no online example (in StackOverflow or otherwise) is ever complete enough to get it working for me (keep in mind I have no actual XML experience, I have of course done the simple tutorials on XPath, XML, and such, but we all know how brief and incomplete these tutorials can be).

我现在想在 WebMatrix C# 中执行此操作,因为在服务器端处理它对于用户来说似乎更易于管理且速度更快.

I now want to do this in WebMatrix C#, as handling it on the server side seems easier to manage and faster for the user.

当我尝试使用此代码在线提供的一些示例时:

When I tried to use some of the examples given online using this code:

@using System.Xml.Linq
@{
    var file = XDocument.Load(Server.MapPath(@"/App_Code/Test.xml"));
    var results = from e in file.Root.Elements() 
       select new { Name = e.Element("someValue").Value, Sales = e.Element("someValueTwo").Value };
    var grid = new WebGrid(results);
}

(我根本不需要使用 WebGrid,它只是在示例中)以及一个名为Test.xml 的App_Code 文件夹中保存的测试xml 文档.

(I don't really need to use WebGrid at all, it was just in the example) And a test xml document saved in the App_Code folder named Test.xml.

尝试从 xml 文档中的两个字段读取一些测试值时出错.这里是测试xml文档:

I get an error when trying to read some test values from two fields within the xml document. Here is the test xml document:

<?xml version="1.0" encoding="utf-8" ?>
<someNode>
    <someValue>HEY THERE! I'M XML!</someValue>
    <someValueTwo>Another Value</someValueTwo>
</someNode>

这里是我尝试将值调用到 cshtml 文件中更下方的页面的地方:

And here is where I try to call the values onto the page further down in the cshtml file:

    <p>This should be a fun test!<br/>And the value is...:
        @foreach(var f in results)
        {
            <div>@f.Name</div>
            <div>@f.Sales</div>
        }
    </p>

最后,这是我运行页面时遇到的错误(请记住,在我的站点中的任何地方都没有与此测试或 xml 的使用有关的其他数据、代码或文件 [cs 或其他]):

Lastly, here is the error I get when I run the page (keep in mind, no other data, code, or files [cs or otherwise] pertaining to this test or the use of xml exist anywhere in my site):

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 4:      var file = XDocument.Load(Server.MapPath(@"/App_Code/Test.xml"));
Line 5:      var results = from e in file.Root.Elements() 
Line 6:         select new { Name = e.Element("someValue").Value, Sales = e.Element("someValueTwo").Value };
Line 7:      var grid = new WebGrid(results);
Line 8:  }

(第 6 行是错误所在的行)

(Line 6 is the line it errors on)

我错过了什么?这些例子看起来真的很简单,但很明显.不是.

What am I missing? The examples act like it is really this simple, but obv. it is not.

老实说,我已经了解了足够多的 WebMatrix 知识,可以在不使用 XML 的情况下完成我的目标(我总是可以只使用数据库或渲染页面等),但我对这种标记语言无情地感到厌烦躲避我,只是因为我永远无法从我使用的任何语言(JavaScript、jQuery、C#)中读取 XML 文件.

In all honesty, I have learned enough about WebMatrix to accomplish my objective without the use of XML, (I could always just use a database, or render pages, etc.) but I am kind of sick of this markup language relentlessly eluding me, simply because I can never read from an XML file in any of the languages I use (JavaScript, jQuery, C#).

推荐答案

我发现使用 XmlDocumentXmlNode 取得了更大的成功XmlNodeList 类,并使用 XPath 指定我想要的元素.但是,这需要您对 XML 文件的结构有一定的了解.

I find that I've had much more success using the XmlDocument, XmlNode and XmlNodeList classes, and using XPath to specify which elements I want. However this requires you to have a somewhat intimate knowledge of the XML file's structure.

var file = XmlDocument.Load(Server.MapPath(@"/App_Code/Test.xml"));
var results = file.SelectNodes("someNode/*")

这将 results 设置为包含 someNode 的所有子节点的 XmlNodeList.您可以将 results 作为 XmlNodes 的 List 进行迭代.然后,您可以在每个节点上执行类似于第 2 行中使用的 XPath 查询以获取子节点.

this sets results to be a XmlNodeList containing all subnodes of someNode. You can iterate through results as a List of XmlNodes. You can then on each node perform a similar XPath query to the one used in line 2 in order to get subnodes.

XPath 语法:http://msdn.microsoft.com/en-us/library/aa926473.aspx

对于以下使用 C# 而不是 VB.net,我深表歉意,但我不熟悉 VB 的 foreach 语法

I apologize for using C# instead of VB.net for the following, but I am not familiar with VB's syntax for a foreach

foreach(XmlNode aNode in results){
  string value = aNode.InnerText
}

会给你嘿嘿!我是 XML!"对于 someValue 节点.\

will give you "HEY THERE! I'M XML!" for the someValue node.\

试试这个,根据我在下面评论中链接的内容:

Try this, based on what I linked in my comment below:

@using System.Xml.Linq
@{
    var file = XmlDocument.Load(Server.MapPath(@"/App_Code/Test.xml"));
    var results = file.SelectNodes("someNode/*");
       select new { Name = e.Element("someValue").Value, Sales = e.Element("someValueTwo").Value };
    /*Do something with each node*/
    foreach(XmlNode aNode in results)
    {
       string value = aNode.InnerText
    }
}

这篇关于如何在 WebMatrix razor (C#) 中使用 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆