English 中文(简体)
阅读Xml文档?
原标题:Reading xml file?
  • 时间:2011-10-15 07:50:13
  •  标签:
  • c#
  • xml

我有这份xml文件,即以务实的方式利用C#:-

<Years>
 <Year Year="2011">
  <Month Month="10">
    <Day Day="10" AccessStartTime="01:15 PM" ExitTime="01:15 PM" />
    <Day Day="11" AccessStartTime="01:15 PM" ExitTime="01:15 PM" />
    <Day Day="12" AccessStartTime="01:15 PM" ExitTime="01:15 PM" />
    <Day Day="13" AccessStartTime="01:15 PM" ExitTime="01:15 PM" />
   </Month>
   <Month Month="11">
    <Day Day="12" AccessStartTime="01:16 PM" ExitTime="01:16 PM" />
   </Month>
  </Year>
</Years>

我在想从中获取具体数据时遇到问题,一是使用XmlReader,或者说,如果读者读一个单行,就会造成错误的方式,一是想在一个具体月份和一年中获得所有天数的清单。

最佳回答

使用<代码>Linq-XML 或公布你所尝试的法典。

var list = from ele in XDocument.Load(@"c:filename.xml").Descendants("Year")
           select new  
                   {
                       Year = (string)ele.Attribute("Year"),
                       Month= (string)ele.Element("Month").Attribute("Month"),
                       Day = (string)ele.Element("Month").Element("Day").Attribute("Day")
                   };
foreach (var t in list)
  {
     Console.WriteLine(t.Year + " " + t.Month + " " + t.Day );
  }
问题回答

我同意DVD的建议,即使用LINQ到XML。 寻找特定年份和月份的所有天数很简单:

XDocument doc = XDocument.Load("file.xml");
var days = doc.Elements("Year").Where(y => (int) y.Attribute("Year") == year)
              .Elements("Month").Where(m => (int) m.Attribute("Month") == month)
              .Elements("Day");

(假设所有月和年要素都规定了月和年特性)。)

The result is a sequence of the Day elements for the specified month and year.

In most cases I d actually write one method call per line, but in this case I thought it looked better to have one full filter of both element and attribute per line.

请注意,在LINQ中,some查询最终更容易使用查询词,有些可在上文使用的“dot notation” I ve中读取。

你要求解释一部DVD的代码,因此,你可能同样受到地雷的侵扰,而不是解释准则Q至XML和LINQ的界限,即我的代码正在使用。 这些技术将帮助你在整个地方的代码。

Take a look at this example how to represent the xml with root node and using xml reader how to get the data ....

using System;
 using System.Xml;

 class Program
 {
  static void Main()
  {
       // Create an XML reader for this file.
       using (XmlReader reader = XmlReader.Create("perls.xml"))
       {
        while (reader.Read())
        {
        // Only detect start elements.
        if (reader.IsStartElement())
        {
            // Get element name and switch on it.
            switch (reader.Name)
            {
            case "perls":
                // Detect this element.
                Console.WriteLine("Start <perls> element.");
                break;
            case "article":
                // Detect this article element.
                Console.WriteLine("Start <article> element.");
                // Search for the attribute name on this current node.
                string attribute = reader["name"];
                if (attribute != null)
                {
                Console.WriteLine("  Has attribute name: " + attribute);
                }
                // Next read will contain text.
                if (reader.Read())
                {
                Console.WriteLine("  Text node: " + reader.Value.Trim());
                }
                break;
               }
           }
             }
        }
      }
   }

投入案文[每秒]

<?xml version="1.0" encoding="utf-8" ?>
  <perls>
    <article name="backgroundworker">
    Example text.
    </article>
    <article name="threadpool">
    More text.
    </article>
    <article></article>
    <article>Final text.</article>
  </perls>

产出

Start <perls> element.
Start <article> element.
  Has attribute name: backgroundworker
  Text node: Example text.
Start <article> element.
  Has attribute name: threadpool
  Text node: More text.
Start <article> element.
  Text node:
Start <article> element.
  Text node: Final text.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签