0

I am working on a C# and Android library project. What I am basically trying to do is write a library in Android that will send me crash details onto my server.

I then have a C# console application that runs on my server and processes the data received by Android and from this data I want to generate an XML file, so that another program can read in the XML file and provide a monthly report.

I've got stuck with the best way of writing and reading the XML though.

I've read a lot about it and found various things such as XMLWriter or XMLSerializer but I don't know which works best, nor do I understand entirely how these are implemented.

Below is a basic design of how the XML file should be written, this is what I've written manually to give an understanding of what I want to achieve.

<?xml version="1.0" encoding="utf-8" ?>
<apps>
  <app>
    <MyApp>
      <appID>1</appID>
      <applicationID>0027598641</applicationID>
      <platform>Android</platform>
      <CrashDetails>
        <Exceptions>
          <Exception>
            <CrashID>55</CrashID>
            <ExceptionType>NullPointerException</ExceptionType>
            <FullException>NullPointerException at line 2</FullException>
            <StartDate>01-11-2013 09:52:00</StartDate>
            <EndDate>02-11-2013 14:43:13</EndDate>
            <AppVersionName>6.1.1.6</AppVersionName>
            <stacktrace>NullPointerException at line 2 com.MyCompany.MyApp.MyClass.MyMethod</stacktrace>
            <Severity>Critical</Severity>
            <OccurrenceCount>9</OccurrenceCount>
          </Exception>
          <Exception>
            <CrashID>56</CrashID>
            <ExceptionType>NullPointerException</ExceptionType>
            <FullException>NullPointerException at line 2</FullException>
            <StartDate>01-11-2013 09:52:00</StartDate>
            <EndDate>02-11-2013 14:43:13</EndDate>
            <AppVersionName>6.1.1.6</AppVersionName>
            <stacktrace>NullPointerException at line 2 com.MyCompany.MyApp.MyClass.MyMethod</stacktrace>
            <Severity>Critical</Severity>
            <OccurrenceCount>9</OccurrenceCount>
          </Exception>
        </Exceptions>
      </CrashDetails>
    </MyApp>
    <MyApp1>
      <appID>2</appID>
      <applicationID>4844354</applicationID>
      <platform>Android</platform>
      <CrashDetails>
        <Exceptions>
          <Exception>
            <CrashID>55</CrashID>
            <ExceptionType>NullPointerException</ExceptionType>
            <FullException>NullPointerException at line 2</FullException>
            <StartDate>01-11-2013 09:52:00</StartDate>
            <EndDate>02-11-2013 14:43:13</EndDate>
            <AppVersionName>6.1.1.6</AppVersionName>
            <stacktrace>NullPointerException at line 2 com.MyCompany.MyApp.MyClass.MyMethod</stacktrace>
            <Severity>Critical</Severity>
            <OccurrenceCount>9</OccurrenceCount>
          </Exception>
        </Exceptions>
      </CrashDetails>
    </MyApp1>
  </app>
</apps>

Thanks for any help you can provide.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 3
    XMLSerializer is used when you want to convert an object into XML, XMLWriter is used when you build your deisred XML document in memory via node additions, etc. then you want to write the document. – Gary Walker Nov 14 '13 at 21:42
  • There is a wealth of approaches to this in [this](http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files?rq=1) post. – Brian Nov 14 '13 at 21:43

2 Answers2

1

Our team typically uses LINQ to XML, which provides a really powerful way to work with XML data (including loading XML from files, parsing XML streams, creating XML document and writing/saving XML to files.)

The following link provides a good overview of LINQ to XML
http://www.dreamincode.net/forums/topic/218979-linq-to-xml/

In addition, you may find “the XML part” section of the following page helpful
http://www.codeproject.com/Articles/24376/LINQ-to-XML

Regards

Seymour
  • 7,043
  • 12
  • 44
  • 51
0

Personally, I prefer to use LINQ to XML for any XML related stuff in C#. You can create XML Trees, and then persist/serialize those trees to file, XmlWriter, or other types of streams.

But in cases when I need huge performance, I prefer to create XML using StringBuilder class and string concatenation operations.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188