开发者

Can I get a specific example of XML in c#

I am trying to pull mu开发者_StackOverflow中文版ltiple elements out of an XML documents and their children but I cannot find a useful example anywhere... MSDN is very vague. This is c# in .Net

I am creating this XML dynamically already and transferring it to a string. I have been trying to use XmlNode with a NodeList to go through each file in a foreach section but It is not working properly.

Here is some sample 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 each of the full paths <result>


There are multiple ways to solve this problem, depending on which version of the .NET Framework you are working on:

.NET 1.x, 2.0 and 3.0

You can easily obtain a filtered list of nodes from your XML document by issuing an XPath query via the XPathDocument class:

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 and later

You should use LINQ to XML to query and filter XML hierarchies, since it offers a much more expressive API than the XPath syntax:

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);
}

Related resources:

  • How to query XML with an XPath expression by using Visual C#
  • LINQ to XML Overview
  • Comparison of XPath and LINQ to XML


If your document is not too big (I think it's not, as you're already generating it dinamically), couldn't you simply use LINQ to Xml?

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


You can use XElement.Parse() ( or XDocument.Parse(), but I use it only when you have a proper xml document )

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);
}


I am creating this XML dynamically already and transferring it to a string. ... I need to pull each of the full paths <result>

If you just need the serealized <result> elements of the XML document, probably the simplest way to do this is with this XSLT transformation (read your MSDN documentation for examples how to perform an XSLT transformation (the XslCompiledTransform.Transform() method)) in C#:

<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>

When applied on the provided XML document:

<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>

the wanted, correct result is produced:

<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>


You should probably be using Linq to Xml or the XmlDocument API.

With XmlDocument you can do:

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

or with linq to xml (XDocument api)

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜