Blank node Wrap line to solve the problem in time for the Winform xml file is saved

Scenes

In Winform custom xml configuration file reading and writing the nodes:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100532137

When implemented on xml configuration file for reading and writing the node above, find a problem,

Is the content of the node is null the corresponding xml file node wrap, then use the next time you read

InnerText will read \ r \ n symbols and the like.

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.

achieve

Save method is used:

 public static Boolean  WriteXml(XmlDocument xml , string filePath)
        {
            try
            {
                xml.Save(filePath);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

 

If the display style does not require configuration file, you can

using (XmlTextWriter xtw = new XmlTextWriter(file, null))  
{  
    xtw.Formatting = Formatting.None;  
    XmlDoc.Save(xtw);  
}

 

Achieved, but this will cause all nodes xml sequentially arranged, the structure does not appear as nodes

If there is necessary to maintain the structure, but also empty node xml file does not produce wrap, you can assign when an empty node, giving

A space.

if (yAxisModelList[no - 1].Title.ToString().Equals("") || yAxisModelList[no - 1].Title == null)
                {
                    xmlNode.SelectSingleNode("title").InnerText = " ";
                    xmlNode.SelectSingleNode("titleKey").InnerText = " ";
                }
                else
                {
                    xmlNode.SelectSingleNode("title").Inner Text = yAxisModelList [no - 1].Title;
                    xmlNode.SelectSingleNode("titleKey").InnerText = yAxisModelList[no - 1].TitleKey;
                }

 

Guess you like

Origin www.cnblogs.com/badaoliumangqizhi/p/11583992.html