问题
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).
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);
}
(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.
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>
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>
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: }
(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.
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#).
回答1:
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/*")
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 Syntax: http://msdn.microsoft.com/en-us/library/aa926473.aspx
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
}
will give you "HEY THERE! I'M XML!" for the someValue
node.\
Edit: 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
}
}
来源:https://stackoverflow.com/questions/13162518/how-to-use-xml-with-webmatrix-razor-c