SoFunction
Updated on 2025-03-01

Detailed explanation of XML and JSON data processing cases in C#

1. XML data processing

1.1 What is XML?

XML (Extensible Markup Language) is a markup language similar to HTML, but it focuses more on the structured representation of data than on display. XML files are often used to store and transfer data.

1.2 How to read an XML file?

In C#, you can useClasses in the namespace to manipulate XML files. Here is a simple example that demonstrates how to read an XML file:

using System;
using ;

class Program
{
    static void Main()
    {
        XmlDocument xmlDoc = new XmlDocument();
        ("");

        XmlNodeList nodeList = ("book");
        foreach (XmlNode node in nodeList)
        {
            ("Title: " + node["title"].InnerText);
            ("Author: " + node["author"].InnerText);
        }
    }
}

1.3 How to create and write XML files?

Creating an XML file and writing data is just as simple as this:

XmlDocument doc = new XmlDocument();
XmlElement root = ("books");
(root);

XmlElement book = ("book");
XmlElement title = ("title");
 = "C# Primer";
(title);

XmlElement author = ("author");
 = "John Doe";
(author);

(book);

("");

1.4 FAQs and Solutions

  • question: Loading the entire file into memory can cause performance problems when the XML file is large.

    • Solution: useXmlReaderThe class performs streaming reading, which can reduce memory usage.
  • question: XML file format error results in parsing failure.

    • Solution: When writing or modifying an XML file, use the verification tool to ensure it is formatted correctly.

2. JSON data processing

2.1 What is JSON?

JSON (JavaScript object notation) is a lightweight data exchange format that is easy to read and write by people, and is also easy to machine parse and generate.

2.2 How to read JSON data?

In C#, it is usually usedLibrary to process JSON data. First, the installation package is required.

dotnet add package 

Then, you can parse the JSON string using the following code:

using ;

string json = "{"name":"John", "age":30}";
JObject obj = (json);
(obj["name"]); // Output: John(obj["age"]);  // Output: 30

2.3 How to serialize an object to JSON?

Serializing an object to JSON is very simple:

var person = new { name = "John", age = 30 };
string jsonString = (person);
(jsonString); // Output: {"name":"John","age":30}

2.4 FAQs and Solutions

  • question: When serializing complex objects, you may encounter problems with circular references.

    • Solution: Can be setAttributes to handle circular references.
  • question: JSON data is incomplete or incorrect in format resulting in parsing failure.

    • Solution: Strict format checks are carried out before sending JSON data, and the receiver should also have an exception handling mechanism.

3. Summary

Whether it is XML or JSON, there are applicable scenarios. Which format to choose depends on the specific application requirements. For C# developers, it is very important to master the basic operations of these two formats. Hopefully this article helps you better use XML and JSON in your actual project.

The above is the detailed explanation of XML and JSON data processing in C#. For more information about C# XML and JSON data processing, please pay attention to my other related articles!