---
title: "Why Dataset.readxml doesn't trim whitespace?"  
description: "Why Dataset.readxml doesn't trim whitespace?"  
author: "Utpal Vishwas"  
published: 2023-08-29  
updated: 2023-09-26  
canonical: https://www.mindstick.com/forum/159711/why-dataset-readxml-doesn-t-trim-whitespace  
category: "c#"  
tags: ["c#", "xml"]  
reading_time: 2 minutes  

---

# Why Dataset.readxml doesn't trim whitespace?

Why Dataset.readxml doesn't trim whitespace?

## Replies

### Reply by Aryan Kumar

The **DataSet.ReadXml** method in .NET does not trim whitespace by default because it aims to faithfully represent the XML data it reads, including any leading or trailing whitespace within the XML elements. The method is designed to maintain the integrity of the XML structure and content as it is stored in the **DataSet**.

If you want to trim whitespace from the data after reading it into a **DataSet**, you can do so manually by iterating through the rows and columns of the **DataSet** and applying the **Trim()** method or a custom trimming function to individual cell values. Here's an example of how you can achieve this:

```plaintext
DataSet dataSet = new DataSet();
dataSet.ReadXml("yourXmlFile.xml");

// Iterate through each DataTable in the DataSet
foreach (DataTable table in dataSet.Tables)
{
    // Iterate through each DataRow in the DataTable
    foreach (DataRow row in table.Rows)
    {
        // Iterate through each DataColumn in the DataRow
        for (int i = 0; i < table.Columns.Count; i++)
        {
            // Trim whitespace from the cell value
            row[i] = row[i].ToString().Trim();
        }
    }
}

// Now the DataSet contains data with leading and trailing whitespace trimmed
```

This code snippet reads an XML file into a **DataSet** and then iterates through each cell value, applying the **Trim()** method to remove leading and trailing whitespace. After this operation, the **DataSet** will contain the trimmed data.

Keep in mind that trimming whitespace might be necessary in certain scenarios to ensure consistency and data quality, but it's not always appropriate for all data sets, so the decision to trim whitespace should be made based on your specific requirements.


---

Original Source: https://www.mindstick.com/forum/159711/why-dataset-readxml-doesn-t-trim-whitespace

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
