SoFunction
Updated on 2025-03-07

C# Read and write XML operation code instance through XML node attributes/attribute values

The contents are as follows:

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <title>
<settings id = "0" name = "Show text">Welcome! Smart service, interactive experience...</settings>
<settings id = "1" name = "font">Microsoft Yahei</settings>
<settings id = "2" name = "Color">Yellow</settings>
<settings id = "3" name = "Font size">48</settings>
  </title>
  <menu>
<submu name="Department Distribution"/>
<submu name="Promotional Video"/>
<submu name="Department Promotion"/>
<submu name="Conference Schedule"/>
  </menu>
  <mu1>
<submu id = "0" name = "iCentroView Product">
      <video id = "0">Videos/ICV.mp4</video>
    </submu>
<submu id = "1" name = "employee community">
      <video id = "0">Videos/ygsqxcp.mp4</video>
    </submu>
<submu id = "2" name = "3D display">
      <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>
</root>

2. Obtain XML document

Copy the codeThe code is as follows:

private static string url = +"Config\\";
        private  XmlDocument xmlDoc;
        private XmlNode root;
        public static string Title;
        public  XMLHandler()
        {
            xmlDoc = new XmlDocument();
            LoadConfig();
        }

        private  void LoadConfig()
        {
            try
            {
                (url);
                root = ("root");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

3. Read the content in the XML node through the attribute name

Copy the codeThe code is as follows:

public TitleModel GetTitle()
        {
            try
            {
                TitleModel title = new TitleModel();
                XmlNode node = ;
                if(node!=null)
                {
                    foreach (XmlNode nd in )
                    {
                        XmlElement element = (XmlElement)nd;
if (("name") == "Show text")
                        {
                            = ;
                        }
else if (("name") == "Font Size")
                        {
                            = Convert.ToInt32();
                        }
else if (("name") == "Color")
                        {
                            = FontColor();
                        }
else if (("name") == "Font")
                        {
                            = ;
                        }
                    }
                }
                return title;       
            }
            catch (Exception e)
            {       
                throw e;
            }

        }

4. Read the attribute value of nodes in XML through attributes

Copy the codeThe code is as follows:

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

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

Copy the codeThe code is as follows:

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

        }