C# Best way to store xml data with in xml

T.Zacks 3,996 Reputation points
2022-11-22T17:12:38.233+00:00

I have a datatable and populate that datatable. after that i save that datatable content to xml file and found xml data with in xml file looks funny.
see how i work.

DataSet dsSettings = new DataSet();  
DataTable dtSettings = new DataTable();  
  
dtSettings.Columns.Add("Ticker", typeof(string));  
dtSettings.Columns.Add("ClientCode", typeof(string));                          
dtSettings.Columns.Add("xml", typeof(string));  

dr = dtSettings.NewRow();  
dr["Ticker"] = strTicker;  
dr["ClientCode"] = strclientcode;  
dr["xml"] = xml;  
  
dtSettings.Rows.Add(dr);  
dsSettings.Tables.Add(dtSettings);  
CSMSettingsFile = @"C:\aa\a.xml";  
dsSettings.WriteXml(CSMSettingsFile);  

now see how xml data looks like in xml file. instead of tag now this kind of characters are showing < how to get rid of this problem?

<?xml version="1.0" standalone="yes"?>  
<NewDataSet>  
  <Table1>  
    <Ticker>FLNC</Ticker>  
    <ClientCode>FLNC</ClientCode>  
    <xml>&lt;?xml version="1.0" encoding="utf-16"?&gt;  
&lt;root&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2679&lt;/ID&gt;  
    &lt;TabName&gt;Disclosure&lt;/TabName&gt;  
    &lt;DisplayName&gt;Disclosure&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;1&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2680&lt;/ID&gt;  
    &lt;TabName&gt;Summary&lt;/TabName&gt;  
    &lt;DisplayName&gt;Summary&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;2&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2681&lt;/ID&gt;  
    &lt;TabName&gt;Model&lt;/TabName&gt;  
    &lt;DisplayName&gt;Vertical&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;3&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2682&lt;/ID&gt;  
    &lt;TabName&gt;Horizontal&lt;/TabName&gt;  
    &lt;DisplayName&gt;Horizontal&lt;/DisplayName&gt;  
    &lt;Selected&gt;false&lt;/Selected&gt;  
    &lt;Order&gt;4&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2696&lt;/ID&gt;  
    &lt;TabName&gt;Delta&lt;/TabName&gt;  
    &lt;DisplayName&gt;Delta&lt;/DisplayName&gt;  
    &lt;Selected&gt;false&lt;/Selected&gt;  
    &lt;Order&gt;5&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2683&lt;/ID&gt;  
    &lt;TabName&gt;Dashboard&lt;/TabName&gt;  
    &lt;DisplayName&gt;Dashboard&lt;/DisplayName&gt;  
    &lt;Selected&gt;false&lt;/Selected&gt;  
    &lt;Order&gt;6&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2684&lt;/ID&gt;  
    &lt;TabName&gt;Internal&lt;/TabName&gt;  
    &lt;DisplayName&gt;Internal&lt;/DisplayName&gt;  
    &lt;Selected&gt;false&lt;/Selected&gt;  
    &lt;Order&gt;7&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2685&lt;/ID&gt;  
    &lt;TabName&gt;Valuation&lt;/TabName&gt;  
    &lt;DisplayName&gt;Valuation&lt;/DisplayName&gt;  
    &lt;Selected&gt;false&lt;/Selected&gt;  
    &lt;Order&gt;8&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2686&lt;/ID&gt;  
    &lt;TabName&gt;Notes&lt;/TabName&gt;  
    &lt;DisplayName&gt;Notes&lt;/DisplayName&gt;  
    &lt;Selected&gt;false&lt;/Selected&gt;  
    &lt;Order&gt;9&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2733&lt;/ID&gt;  
    &lt;TabName&gt;FLNC_Summary_Segment_Revenues&lt;/TabName&gt;  
    &lt;DisplayName&gt;Summary Segment Revenues&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;10&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2734&lt;/ID&gt;  
    &lt;TabName&gt;FLNC_Summary_P&amp;L&lt;/TabName&gt;  
    &lt;DisplayName&gt;Summary P&amp;L&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;11&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2735&lt;/ID&gt;  
    &lt;TabName&gt;FLNC_P&amp;L_Comparison&lt;/TabName&gt;  
    &lt;DisplayName&gt;P&amp;L Comparison&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;12&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
  &lt;Alltabs&gt;  
    &lt;ID&gt;2736&lt;/ID&gt;  
    &lt;TabName&gt;FLNC_Metrics&lt;/TabName&gt;  
    &lt;DisplayName&gt;Metrics&lt;/DisplayName&gt;  
    &lt;Selected&gt;true&lt;/Selected&gt;  
    &lt;Order&gt;13&lt;/Order&gt;  
  &lt;/Alltabs&gt;  
&lt;/root&gt;</xml>  
  </Table1>  
</NewDataSet>  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,516 Reputation points Microsoft Vendor
    2022-11-23T03:06:13.73+00:00

    @T.Zacks , Welcome to Microsoft Q&A, you could try to use system.web.httputility.htmldecode to replace some special characters.

    Here is a code example you could refer to.

        var CSMSettingsFile = "a.xml";  
        dsSettings.WriteXml(CSMSettingsFile);  
        var content = File.ReadAllText(CSMSettingsFile);  
        string encodedXml = HttpUtility.HtmlDecode(content);  
        File.WriteAllText(CSMSettingsFile, encodedXml);  
    

    Tested result:

    263224-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and 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

0 additional answers

Sort by: Most helpful

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.