C# XML Parser

Vardyschon18 1 Reputation point
2022-02-06T19:42:25.48+00:00

I Implemented a XML parser in Java a while back, but I cant figure out how to do it in C#, as I am relatively new. Is there a similiar approach to this in C#?
My Parser should convert simple nodes from an XML file into objects. I am going to implement this into an WCF Service if that is relevant.

public class XMLParser {
Document document;

public XMLParser(String path) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new File(path));
    } catch (ParserConfigurationException | IOException | SAXException e) {
        e.printStackTrace();
    }
}


public ArrayList<RegisterEntry> getContent() {
    ArrayList<RegisterEntry> entries = new ArrayList<>();
    NodeList list = document.getElementsByTagName("RegisterEntry");
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        Element e = (Element) node;
        int id = Integer.parseInt(e.getAttribute("entryID"));
        String dateString = (e.getElementsByTagName("Date").item(0).getTextContent());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime date = LocalDateTime.parse(dateString, formatter);
        String entryType = e.getElementsByTagName("EntryType").item(0).getTextContent();
        String entryText = e.getElementsByTagName("EntryText").item(0).getTextContent();
        String teacher = e.getElementsByTagName("Teacher").item(0).getTextContent();
        RegisterEntry entry = new RegisterEntry(id, entryType, teacher, entryText, date);
        entries.add(entry);
    }
    return entries;
}
Developer technologies .NET Other
Developer technologies C#
{count} votes

4 answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2022-02-06T19:49:17.88+00:00

    You can see MSDN samples, like at :
    XmlDocument Class

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-02-06T21:30:42+00:00

    You can also look at xml serialization, which maps xml into poco objects

    https://learn.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization

    0 comments No comments

  3. Sam of Simple Samples 5,546 Reputation points
    2022-02-07T00:45:18.84+00:00

    If you have sample XML then copy a complete (valid) sample to the clipboard then in a C# file go to the VS Edit menu and there is a special paste command for pasting the XML as a class. That will very conveniently generate C# classes. Then it is easy to use the JsonSerializer Class. In the past Newtonsoft was popular but Microsoft has improved .Net.

    0 comments No comments

  4. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-02-07T02:18:37.147+00:00

    Hi @Vardyschon18 ,
    To implement xml serialization in WCF services, you can see the following two documents.
    By default, WCF uses the DataContractSerializer class to serialize data types, WCF also supports the XmlSerializer class.
    Using the XmlSerializer Class.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-the-xmlserializer-class
    The XmlSerializer sample demonstrates how to serialize and deserialize XmlSerializer-compatible types.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/xmlserializer-sample
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.