[英]How to get the value from XML?
I'm making a small tool for checking the material number from the XML file. 我正在制作一个小工具来检查XML文件中的材料编号。
I know this quite easy for you experts and I would like to ask for your help on this to get me started on this. 我知道这对你的专家来说很容易,我想请你帮忙解决这个问题。 On my machine I have .NET 2.0 framework I guess, and VS C# Express 2005 installed.
在我的机器上,我想有.NET 2.0框架,并安装了VS C#Express 2005。
I have an XML that contains data of a material. 我有一个包含材料数据的XML。 It is located at my local drive.
它位于我当地的车道。 I am able now to browse the XML file and save the file in a string variable.
我现在能够浏览XML文件并将文件保存在字符串变量中。 Well, that's what I have done so far..
嗯,这就是我到目前为止所做的......
if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "Product.xml");
string prodFile = files[0];
...
...
Suppose this is the structure of the XML: 假设这是XML的结构:
<Record>
<Product>
<Material_Number>20209485</Material_Number>
<Product_Type>Type1</Product_Type>
...
...
</Product>
</Record>
How can I get the material number value? 如何获得物料编号值?
You can use the XmlDocument
class for loading your XML File into a DOM. 您可以使用
XmlDocument
类将XML文件加载到DOM中。
MSDN - This class implements the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. The DOM is an in-memory (cache) tree representation of an XML document and enables the navigation and editing of this document.
MSDN - 此类实现W3C文档对象模型(DOM)1级核心和核心DOM级别2. DOM是XML文档的内存(缓存)树表示,可以导航和编辑此文档。 Because XmlDocument implements the IXPathNavigable interface it can also be used as the source document for the XslTransform class.
因为XmlDocument实现了IXPathNavigable接口,所以它也可以用作XslTransform类的源文档。
There are many ways to read your value. 有很多方法可以读懂你的价值。 I really encourage you to read Working with Xml DOM
我真的很鼓励你阅读使用Xml DOM
XmlNodeList list = xml.GetElementsByTagName("Product");
XmlAttributeCollection attr = list[0].Attributes;
string materialNumber = list[0].ChildNodes[0].InnerText;
or 要么
XmlNodeList list = xml.GetElementsByTagName("Material_Number");
string materialNumber = list[0].InnerText;
You could also use XPathNavigator
and XPathExpression
with XmlDocument
. 你也可以使用
XPathNavigator
和XPathExpression
与XmlDocument
。
var xmlDoc = new XmlDocument();
xmlDoc.Load("Product.xml") //or xmlDoc.LoadXml(xmlString);
var xmlNav = xmlDoc.CreateNavigator();
string materialNum;
var iterator = xmlNav.Select("/Record/Product/Material_Number");
if (iterator.MoveNext() && iterator.Current != null)
materialNum = iterator.Current.Value;
If you use .Net 3.0+ you could use System.Xml.Linq.XDocument
. 如果您使用.Net 3.0+,则可以使用
System.Xml.Linq.XDocument
。
var xdoc = XDocument.Load("Product.xml"); //or var xdoc = XDocument.Parse(xmlString);
var materialNum = xdoc.Root.Element("Product").Element("Material_Number").Value;
I was able to find a solution. 我找到了解决方案。 Not so elegant though...
虽然不那么优雅......
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load(@"C:\Product.xml");
XmlNodeList a = xmlDoc.GetElementsByTagName("Material_Number");
string materialNumber = a[0].InnerText;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.