C reads and writes XML operation code instances through the XML node property and property value

  • 2020-05-26 09:59:09
  • OfStack

1. The content of XML is as follows:


<?xml version="1.0" encoding="utf-8" ?>
<root>
  <title>
    <settings id = "0" name = " According to the text "> Welcome you! Intelligent service, interactive experience ......</settings>
    <settings id = "1" name = " The font "> Microsoft jas black </settings>
    <settings id = "2" name = " color ">Yellow</settings>
    <settings id = "3" name = " The font size ">48</settings>
  </title>
  <menu>
    <submu id="0" name=" Department of distribution "/>
    <submu id="1" name=" Promotional video "/>
    <submu id="2" name=" Department of propaganda "/>
    <submu id="3" name=" The arrangements for the meeting "/>
  </menu>
  <mu1>
    <submu id = "0" name = "iCentroView product ">
      <video id = "0">Videos/ICV.mp4</video>
    </submu>
    <submu id = "1" name = " Staff community ">
      <video id = "0">Videos/ygsqxcp.mp4</video>
    </submu>
    <submu id = "2" name = "3 D show ">
      <video id = "0">Videos/iBaosight.mp4</video>
    </submu>
    <submu id = "1" name = " Good life promotion ">
      <video id = "0">Videos/goodlift.mp4</video>
    </submu>
  </mu1>
  <main>Picture/main.jpg</main> 
</root>

2. Get the XML document


private static string url = AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"Config\\config.xml";
        private  XmlDocument xmlDoc;
        private XmlNode root;
        public static string Title;
        public  XMLHandler()
        {
            xmlDoc = new XmlDocument();
            LoadConfig();
        }
        private  void LoadConfig()
        {
            try
            {
                xmlDoc.Load(url);
                root = xmlDoc.SelectSingleNode("root");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

3. Read the contents of the XML node by the property name


public TitleModel GetTitle()
        {
            try 
            {
                TitleModel title = new TitleModel();
                XmlNode node = root.FirstChild;
                if(node!=null)
                {
                    foreach (XmlNode nd in node.ChildNodes)
                    {
                        XmlElement element = (XmlElement)nd;
                        if (element.GetAttribute("name") == " According to the text ")
                        {
                            title.Title = nd.InnerText; 
                        }
                        else if (element.GetAttribute("name") == " The font size ")
                        {
                            title.FontSize = Convert.ToInt32(nd.InnerText);
                        }
                        else if (element.GetAttribute("name") == " color ")
                        {
                            title.FontColor = FontColor(nd.InnerText);
                        }
                        else if (element.GetAttribute("name") == " The font ")
                        {
                            title.FontFamily = nd.InnerText;
                        }
                    }
                }
                return title;        
            }
            catch (Exception e)
            {        
                throw e;
            }

        }

4. Read the attribute value of the node in XML by the attribute


public List<string> GetMenuString()
        {
            try
            {
                List<string> list=new List<string>();
                XmlNode menu = root.ChildNodes[1];
                if (menu != null)
                {
                    foreach (XmlNode node in menu.ChildNodes)
                    {
                        XmlElement element = (XmlElement)node;
                        list.Add(element.GetAttribute("name"));
                    }
                }
                return list;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

5. Get the content in the XML node through the node


public string GetMainBackground()
        {
            string url ="Images/mainjpg";
            try
            {
                XmlNode node = root.LastChild;
                if (node != null)
                {
                    url =  node.InnerText;
                }
                return url;
            }
            catch (Exception e)
            {
                throw e;
            }

        }


Related articles: