阅读XML文件部分 [英] Read A XML File Section

查看:81
本文介绍了阅读XML文件部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件



 <? < span class =code-summarycomment> xml     version   =  1.0   编码  =  utf-8  >  
< 开始 >
< 1 >
< > 1 < / Key >
< 文字 > 世界< < span class =code-leadattribute> / Text >
< / 1 >

< 2 >
< > 2 < / Key >
< 文字 > Hi World < /文字 >
< / 2 >
< /开始 >





我想要的代码只能用于读取

 <   1  >  <  文字 >  <   / 1  >  

仅限部分...并在消息框中显示结果。 />


将会有另一个按钮2的代码



如果我点击那里它会被读取

< 2 > < 文字 > < / 2 >

仅限部分并在消息框上显示结果



感谢高级。

解决方案

提供了不同的XML解析器。 NET FCL。允许您在没有开销的情况下跳过部分数据并仅提取所需数据的方法是由此类实现的:

https://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110% 29.aspx [ ^ ]。



-SA


首先,您的XML节点不能以您拥有的数字开头。请查看 XML Elements [ ^ ]并查看 XML命名规则。您将需要执行以下操作:

 <?  xml     version   =  1.0    encoding   =  utf-8  >  
< 开始 >
< 节点 >
< < span class =code-keyword>> 1 < / Value >
< 文字 > 世界< /文字 >
< / Node >
< 节点 >
< ; > 2 < /值 >
< ; 文字 > Hi World < /文字 >
< / Node >
< /开始 >





其次,RyanDev指示你使用 XDocument 供你研究和学习,以便有人把这样的代码放在:

  var  documentRoot = XDocument.Load( @  C:\ MyXmlFile.xml)。Root; 

var textNode =(来自节点 in documentRoot.Elements( Node
其中 nodes.Element( Value)。 == 1
选择 nodes.Element( Text))。FirstOrDefault();

Console.WriteLine(textNode.Value);





您将能够理解它。 .NET中的 XDocument 非常易于使用和理解。我建议您花一些时间来学习它。对于小型XML文档,我只是使用它。以下是一些可以帮助您入门的教程。



如何开始使用XML和LINQ:初学者指南。 [ ^ ]

< a href =http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html>了解C#:简单的Linq到XML查询 [ ^ ]


 <! -    ?xml version =1.0encoding =utf-8?   - >   
< 开始 >
< val c = AMP >
< key > < / key >
< string > AAA < / string >
< / val < span class =code-keyword>>
< / st art >

那么如何在Messagebox中读取字符串AAA?


I have a xml file

<?xml version="1.0" encoding="utf-8"?>
<Start>
  <1>
    <Value>1</Key>    
  <Text>Hi World</Text>
</1>

  <2>
    <Value>2</Key>    
  <Text>Hi World</Text>
</2>
</Start>



I want a code that i can use for only read

<1> <Text> </1>

section only...and show result on message box.

there will be another code for button 2

and if i click on there it will be read

<2> <Text> </2>

section only and show result on messagebox

thanks in advanced.

解决方案

There are different XML parsers offered by .NET FCL. The one which allows you to skip some part of data without overhead and extract only the data you need is implemented by this class:
https://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110%29.aspx[^].

—SA


First, your XML nodes can't start with number like what you have. Take a look at the XML Elements[^] at W3 Schools and look at the XML Naming Rules. You will need to do something like this:

<?xml version="1.0" encoding="utf-8"?>
<Start>
	<Node>
		<Value>1</Value>
		<Text>Hi World</Text>
	</Node>
	<Node>
		<Value>2</Value>
		<Text>Hi World</Text>
	</Node>
</Start>



Second, RyanDev was pointing you to use the XDocument for you to research and learn so that when someone puts code like this:

var documentRoot = XDocument.Load(@"C:\MyXmlFile.xml").Root;

var textNode = (from nodes in documentRoot.Elements("Node")
                where nodes.Element("Value").Value == "1"
                select nodes.Element("Text")).FirstOrDefault();

Console.WriteLine(textNode.Value);



You would be able to understand it. The XDocument in .NET is very easy to use and understand. I recommend that you take some time to learn it. For small XML documents, it is all I use. Below are a few tutorials that should get you started.

How to Start with XML and LINQ: A Beginner Guide.[^]
Understanding C#: Simple Linq to XML Queries[^]


<!--?xml version="1.0" encoding="utf-8"?-->
<start>
    <val c="AMP">
      <key>A</key>
      <string>AAA</string>
    </val>
</start>

then how do i can read String AAA in Messagebox ?


这篇关于阅读XML文件部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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