阅读XML文件全部部分 [英] Read A XML File All Section
问题描述
您好,
在我过去的XML问题中,我得到了很好的回复。但是我也有点问题。
我有这样的XML文件:
< pre lang =xml> < 开始 >
< Apple > ;
< 字符串 > < / String > ;
< / Apple >
< Banana >
< 字符串 > B < / String >
< / Banana < span class =code-keyword>>
< JackFruit >
< String > C < / String >
< / JackFruit >
< / Start >
我可以读取所有部分名称 - Apple,Banana,JackFruit使用此代码:
使用( XmlReader reader = XmlReader.Create( @ D:\\ file.xml))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
// 仅在您退货时返回有START标记
开关(reader.Name.ToString())
{
case 开始:
控制台.WriteLine( 元素的名称是: + reader.ReadString());
}
}
}
但是如果我的xml文件格式是如何做同样的事情:
<? xml version = 1.0 编码 = utf-8 ? >
< 开始 >
< < span class =code-leadattribute> CodeProject 代码 = APPLE >
< 字符串 > < / String >
< / CodeProject >
< CodeProject 代码 = Banana >
< String < span class =code-keyword>> B < / String >
< / CodeProject >
< CodeProject 代码 = JackFruit >
< 字符串 > C < / String >
< / CodeProject >
< / Start >
我想阅读所有部分名称并在listbox1上添加所有部分名称。
有谁知道我怎么能这么做呢?
先谢谢你。
请参考以下链接获取示例代码并根据您的要求进行修改。
http://dotnetblue.blogspot.in/2012/03/reading-xml-nodes-attributes-values.html [ ^ ]
https:// msdn。 microsoft.com/en-us/library/acwfyhc7(v=vs.110).aspx [ ^ ]
http://www.functionx.com/csharp3/xml/Lesson04.htm [ ^ ]
另一种选择是使用LINQ to XML。
LINQ to XML通过其属性检索元素值 [ ^ ]
https://msdn.microsoft.com/en-us/library/bb387086.aspx [<一个href =https://msdn.microsoft.com/en-us/library/bb387086.aspx\"target =_ blanktitle =新窗口> ^ ]
Hello,
In my past XML Problem i got a good response. But i have also a little bit problem.
I have a XML File like this :
<Start>
<Apple>
<String>A</String>
</Apple>
<Banana>
<String>B</String>
</Banana>
<JackFruit>
<String>C</String>
</JackFruit>
</Start>
I can read all section name like - Apple,Banana,JackFruit by using this code :
using (XmlReader reader = XmlReader.Create(@"D:\\file.xml"))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
//return only when you have START tag
switch (reader.Name.ToString())
{
case "Start":
Console.WriteLine("Name of the Element is : " + reader.ReadString());
}
}
}
But how to do same thing if my xml file format is :
<?xml version="1.0" encoding="utf-8"?>
<Start>
<CodeProject Code="APPLE">
<String>A</String>
</CodeProject>
<CodeProject Code="Banana">
<String>B</String>
</CodeProject>
<CodeProject Code="JackFruit">
<String>C</String>
</CodeProject>
</Start>
I would like to read all section names and add all section names on listbox1.
Have anyone idea how can i do this easily ?
Thanks in advanced.
Please refer the below links for sample code and modify as per your requirement.
http://dotnetblue.blogspot.in/2012/03/reading-xml-nodes-attributes-values.html[^]
https://msdn.microsoft.com/en-us/library/acwfyhc7(v=vs.110).aspx[^]
http://www.functionx.com/csharp3/xml/Lesson04.htm[^]
Another option is to use LINQ to XML.
LINQ to XML to retrieve an element value by its attribute[^]
https://msdn.microsoft.com/en-us/library/bb387086.aspx[^]
这篇关于阅读XML文件全部部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!