Can I get a concrete XML example in C #

I am trying to pull a few elements from XML documents and their children, but I cannot find a useful example anywhere ... MSDN is very vague. This is C # in .Net

I create this XML dynamically and pass it to a string. I am trying to use an XmlNode with a NodeList to go through each file in the foreach section, but it does not work properly.

Here is an example XML:

<searchdoc>
    <results>
        <result no = "1">
            <url>a.com</url>
            <lastmodified>1/1/1</lastmodified>
            <description>desc1</description>
            <title>title1</title>
        </result>
        <result no = "2">
            <url>b.com</url>
            <lastmodified>2/2/2/</lastmodified>
            <description>desc2</description>
            <title>title2</title>
        </result>
    </results>
</searchdoc>

I need to pull out each of the full paths <result>

+5
source share
5 answers

There are several ways to solve this problem , depending on the version of the .NET Framework :

.NET 1.x, 2.0 and 3.0

XML-, XPath XPathDocument:

using (var reader = new StringReader("<Results><Result>...</Result></Results>"))
{
  var document = new XPathDocument(reader);
  var navigator = document.CreateNavigator();
  var results = navigator.Select("//result");

  while (results.MoveNext())
  {
    Console.WriteLine("{0}:{1}", results.Current.Name, results.Current.Value);
  }
}

.NET 3.5

LINQ to XML XML-, API, XPath:

var document = XDocument.Parse("<Results><Result>...</Result></Results>");
var results = document.Elements("result");

foreach (var item in results)
{
  Console.WriteLine("{0}:{1}", item.Name, item.Value);
}

:

+5

( , , ), LINQ to Xml?

XDocument myDoc = XDocument.Parse(myXmlString);
foreach(var result in myDoc.Descendants("result"))
{
  DoStuffWithTitle(result.Element("Title").Value);
  ...
}
+1

XElement.Parse() ( XDocument.Parse(), , XML-)

string doc = @"<searchdoc>
<results>
<result no = '1'>
<url>a.com</url>
<lastmodified>1/1/1</lastmodified>
<description>desc1</description>
<title>title1</title>
</result>
<result no = '2'>
<url>b.com</url>
<lastmodified>2/2/2/</lastmodified>
<description>desc2</description>
<title>title2</title>
</result>
</results>
</searchdoc>

";
var element = XElement.Parse(doc);
foreach (var result in element.Descendants("result"))
{
   Console.WriteLine(result.Element("url").Value);
}
+1

, Linq Xml API XmlDocument.

XmlDocument :

string xml = GetXmlFromFile();
var xDoc = XmlDocument.Load(xml);
var nodes = xDoc.SelectNodes('//result');

linq xml (XDocument api)

string xml = GetXmlFromFile();
var xDoc = XDocument.Load(xml);

var nodes = from x in xDoc.Descendants("result")
            select x;
+1

XML .... <result>

serealized <result> XML-, , , - XSLT ( MSDN , XSLT ( XslCompiledTransform.Transform())) #:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/*/result"/>
 </xsl:template>
</xsl:stylesheet>

XML-:

<searchdoc>
    <results>
        <result no = "1">
            <url>a.com</url>
            <lastmodified>1/1/1</lastmodified>
            <description>desc1</description>
            <title>title1</title>
        </result>
        <result no = "2">
            <url>b.com</url>
            <lastmodified>2/2/2/</lastmodified>
            <description>desc2</description>
            <title>title2</title>
        </result>
    </results>
</searchdoc>

, :

<result no="1">
  <url>a.com</url>
  <lastmodified>1/1/1</lastmodified>
  <description>desc1</description>
  <title>title1</title>
</result>
<result no="2">
  <url>b.com</url>
  <lastmodified>2/2/2/</lastmodified>
  <description>desc2</description>
  <title>title2</title>
</result>
+1

All Articles