Click here to Skip to main content
14,865,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a xml file

XML
<?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
XML
<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
XML
<2> <Text> </2>
section only and show result on messagebox

thanks in advanced.
Posted
Comments
ZurdoDev 7-Jul-15 12:35pm
   
Use the XDocument class.
DiscoCoder 7-Jul-15 13:15pm
   
I don't know how to do...and also not understanding...if i was know about it a little bit then i may not ask it here.

anyway thanks for your suggestion.
ZurdoDev 7-Jul-15 13:20pm
   
Just google for c# read xml file, or c# xdocument examples. You'll find tons.
DiscoCoder 7-Jul-15 13:36pm
   
Hello Dear,
Thank you again for your suggestion.This time your suggestion works for me.Thank you a lot. Thank you so much..

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("d:\\product.xml");
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Table/Product");
string proID = "", proName = "", price="";
foreach (XmlNode node in nodeList)
{
proID = node.SelectSingleNode("Product_id").InnerText;
proName = node.SelectSingleNode("Product_name").InnerText;
price = node.SelectSingleNode("Product_price").InnerText;
MessageBox.Show(proID + " " + proName + " " + price);
}

but is there any other best way to do same thing ? if yes then pls suggest me.
ZurdoDev 7-Jul-15 13:40pm
   
If you only want a single node you can change your xpath query. Or you can also use XDocument, not XmlDocument. They are different.

You'll want to also add error handling of course in case it doesn't find the path you are expecting.
DiscoCoder 7-Jul-15 13:56pm
   
thanks again for your response.

could you give few line codes pls...if you have 1 min for me.
pls

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
   
Comments
DiscoCoder 7-Jul-15 14:13pm
   
Thank you so much for also your suggestion...
I got my solution...thanks
   
You are welcome, but will you accept the answer formally? This is really the best way to read a fragment of XML.
—SA
virusstorm 7-Jul-15 14:42pm
   
+5 I use the XMLReader on any file where I don't know what the size will be or if the file size is over a few megabytes. The solution I gave was more in reply to the user's comment to RyanDev. I think this user is still struggling with all of this based on some of the comments.
   
Thank you.
—SA
DiscoCoder 7-Jul-15 15:08pm
   
Thank You
   
You are very welcome.
Good luck, call again.
—SA
DiscoCoder 7-Jul-15 15:10pm
   
XML reader ? XML Package ? I means can i get everything there ?
   
I don't understand what you are talking about. I gave you precise reference to what you can use.
—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
<?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:
C#
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[^]
   
Comments
DiscoCoder 7-Jul-15 14:12pm
   
Thank you so much...
DiscoCoder 7-Jul-15 14:23pm
   
Hello ,
could you tell me also what is do for -

<start>
<latter a="dd">
<value>1
<text>Hi World


DiscoCoder 7-Jul-15 14:27pm
   
<pre lang="xml"><!--?xml version="1.0" encoding="utf-8"?-->
<start>
<val c="AMP">
<key>A
<string>AAA



then how do i can read String AAA in Messagebox ?</pre>
virusstorm 7-Jul-15 14:44pm
   
Basically, you need to parse the XML document, either by using the solution I gave or by using Sergey's solution, find your node, extract the value and assign it to the text box. There are a lot of examples here, give it a try and if you are running into a new problem, post a new question.
DiscoCoder 7-Jul-15 14:57pm
   
thank you for your comments. you are absolutely right.

thanks
XML
<!--?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 ?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900