Click here to Skip to main content
15,884,075 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
how can i extract the "data" value form this xml ...

XML
<root>
<FileTransfer i="1" ad="abc">
<pot>250</pot>
<prod>abc</prod>
<des>yoko</des>
</FileTransfer>
<island>
<name>michael</name>
<data>america</data>
</island>
</root>


Please help!!!
Posted

What's wrong with reading standard MSDN documentation? .NET offers different ways to parse XML. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


You don't have to use parsing at XML level. Chances are, you can just serialize and deserialize the data structures you need using XML serialization. The best way to do it is Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Good luck,
—SA
 
Share this answer
 
C#
var document = XDocument.Load(@"D:\test.xml");
          var nodes = document.Descendants().Where(e => e.Name.LocalName.StartsWith("data"));
          var values = nodes.Select(n => n.Value).ToList();


if XML is like this
XML
<products>
  <island>
    <name>AAA</name>
    <data>sfsgsdg</data>
  </island>
  <island>
    <name>BBB</name>
    <data>sgtyeyewy</data>
  </island>
  <island>
    <name>CCC</name>
    <data>rhjfjgfjkgfj</data>
  </island>
</products>


the variable "values" contains list of values e.g {"sfsgsdg","sgtyeyewy","rhjfjgfjkgfj"}
 
Share this answer
 
Use this XML library: http://elmax.codeplex.com/[^]. Note: this library comes in 2 language flavors: C++ and C#

using Elmax;

Element root = new Element();
root.SetDomDoc(doc); // A XML file is read into the DOM doc beforehand.
Element elemData = root["root|island|data"];
if(elemData.Exists)
{
    string data = elemData.GetString("empty");
}


I forgot to show how to load the XML. Here it is from http://elmax.codeplex.com/wikipage?title=CSharp%20Elmax&amp;referringTitle=Documentation#LoadXMLDocument[^]. After calling CreateAndLoadXml, pass the doc to SetDomDoc method.

C#
bool CreateAndLoadXml(out XmlDocument doc, System.String strFilename)
{
    doc = new XmlDocument();
    try
    {
        doc.Load(strFilename);
    }
    catch (System.Exception)
    {
        return false;
    }
    return true;
}
 
Share this answer
 
v4
Just to add to the many different ways to parse XML. Rather than parsing from file, this is parsing as string.

C#
XDocument doc = XDocument.Parse("<root><FileTransfer i=\"1\" ad=\"abc\"><pot>250</pot><prod>abc</prod><des>yoko</des></FileTransfer><island><name>michael</name><data>america</data></island></root>");

            var fileTransElement = doc.Elements("root").Elements("FileTransfer");
            var islandElements = doc.Elements("root").Elements("island");

            foreach (var el in islandElements)
            {
                var nameEl = el.Element("name") == null ? "" : el.Element("name").Value;
                Debug.WriteLine("nameEl: " + nameEl);

                var dataEl = el.Element("data") == null ? "" : el.Element("data").Value;
                Debug.WriteLine("dataEl: " + dataEl);
            }

            foreach (var el in fileTransElement)
            {
                var potEl = el.Element("pot") == null ? "" : el.Element("pot").Value;

                Debug.WriteLine("potEl: " + potEl);
            }




Also Google is your friend...try it[^]
 
Share this answer
 
v3
XmlNodeList nodesData = myxmlDoc.GetElementsByTagName("Data");
string[] islandList=new string[count];
foreach (XmlNode node in nodesData)
{
islandData[i] = node.ChildNodes[0].Value.ToString();
i++;
}
 
Share this answer
 

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