0

I have a text file contains 500+ xmlelements like the following:

<Data a="a" b="b" c="c" d="d"><Date runDt="01-01-1900" /></Data>

Can someone please show me how to read/load it so I can retrieve certain attributes/elements? And once I manipulate the data, write it back to a new text file (and needs to be a .txt file without any xml headers).

Thanks :)

Kyle
  • 406
  • 7
  • 20
  • 3
    You should post the code you've tried and explain what isn't working correctly - and what you'd like to achieve. You're not supposed to ask "please write my code for me" questions on this site. – Stan Shaw Dec 17 '15 at 17:35
  • 2
    Possible duplicate of [How does one parse XML files?](http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files) – Dmitrii Dovgopolyi Dec 17 '15 at 17:35
  • try loding the text in a xmldocument and use xpath for search – Byron Dec 17 '15 at 17:55
  • @Stan, sorry. I did try and wrote something myself. But wasn't successful. What I did was using File.ReadAllLines() to retrieve all these xml like strings line by line. But when I try to convert them to XmlElement, or read them with XmlTextReader I get an error. So I just post the question directly.. – Kyle Dec 17 '15 at 17:56
  • You should post the code you tried and the error message that you received. – Stan Shaw Dec 17 '15 at 17:58

2 Answers2

0

Easiest way is to use:

using System.Xml;

XmlDocument xml = new XmlDocument ();
xml.InnerXml = @"<Data a=\"a\" b=\"b\" c=\"c\" d=\"d\"><Date runDt=\"01-01-1900\" /></Data>";
Console.WriteLine (xml.ChildNodes [0].Attributes [0].InnerText);

Will print

a

Using XmlDocument is very easy, just check its fields, variables and methods.

Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36
  • Thank you. I tried to use XmlDocument.Load() but didn't work cause what I had wasn't a really xml document. Nor using XmlTextReader to parse in the string. – Kyle Dec 17 '15 at 19:34
0

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<Data a=\"a\" b=\"b\" c=\"c\" d=\"d\"><Date runDt=\"01-01-1900\" /></Data>\n" +
                "<Data a=\"a\" b=\"b\" c=\"c\" d=\"d\"><Date runDt=\"01-01-1900\" /></Data>\n" +
                "<Data a=\"a\" b=\"b\" c=\"c\" d=\"d\"><Date runDt=\"01-01-1900\" /></Data>\n" +
                "<Data a=\"a\" b=\"b\" c=\"c\" d=\"d\"><Date runDt=\"01-01-1900\" /></Data>\n" +
                "<Data a=\"a\" b=\"b\" c=\"c\" d=\"d\"><Date runDt=\"01-01-1900\" /></Data>\n";

            //xml can only contain one root tag.  Need to wrap xml in root tag if one is missing
            input = string.Format("<Root>{0}</Root>", input);

            XDocument doc = XDocument.Parse(input);

            // if loading from file
            //string input = File.ReadAllText(filename);
            //input = string.Format("<Root>{0}</Root>", input);
            //XDocument doc = XDocument.Load(filename);

            var results = doc.Descendants("Data").Select(x => new
            {
                a = x.Attribute("a").Value,
                b = x.Attribute("b").Value,
                c = x.Attribute("c").Value,
                d = x.Attribute("d").Value,
                date = DateTime.Parse(x.Element("Date").Attribute("runDt").Value)
            }).ToList();

        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20