none
Load Config.xml get certain Data RRS feed

  • Question

  • I try to get XElement item value and set value

    Below is my structure

    <CONFIG xmlns:xdi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" FrameWidth="400" FrameHeight="200">

    <Frames>
        <FRAME FrameName="Frame0">
          <Frame_Elements>
            <Frame_Element><!--Frame_Element[0]-->
              <Basic><!--Basic[0]-->
                <Id>0</Id>
                <Uid>0</Uid>
              </Basic>
              <Base><!--Base[0]-->
                <BaseFilePath>C:/Image</BaseFilePath>
                <BaseFileName>f.png</BaseFileName>
              </Base>
            </Frame_Element>
            <Frame_Element><!--Frame_Element[1]-->
              <Basic><!--Basic[1]-->
                <Id>1</Id>
                <Uid>0</Uid>
              </Basic>
              <Base><!--Base[1]-->
                <BaseFilePath>C:/Image</BaseFilePath>
                <BaseFileName>large.png</BaseFileName>
              </Base>
            </Frame_Element>
            <Frame_Element><!--Frame_Element[2]-->
              <Basic><!--Basic[2]-->
                <Id>2</Id>
                <Uid>0</Uid>
              </Basic>
              <Base><!--Base[2]-->
                <BaseFilePath>C:/Image</BaseFilePath>
                <BaseFileName>medium.png</BaseFileName>
              </Base>
            </Frame_Element>
            <Frame_Element><!--Frame_Element[0]-->
              <Basic><!--Basic[3]-->
                <Id>3</Id>
                <Uid>0</Uid>
              </Basic>
              <Base><!--Base[3]-->
                <BaseFilePath>C:/Image</BaseFilePath>
                <BaseFileName>small.png</BaseFileName>
              </Base>
            </Frame_Element>
          </Frame_Elements>
        </FRAME>
        <FRAME FrameName="Frame1">
          <Frame_Elements>
            <Frame_Element><!--Frame_Element[4]-->
              <Basic><!--Basic[4]-->
                <Id>4</Id>
                <Uid>0</Uid>
              </Basic>
              <Base><!--Base[4]-->
                <BaseFilePath>C:/Image</BaseFilePath>
                <BaseFileName>Main.png</BaseFileName>
              </Base>
            </Frame_Element>
            <Frame_Element><!--Frame_Element[5]-->
              <Basic><!--Basic[5]-->
                <Id>5</Id>
                <Uid>0</Uid>
              </Basic>
              <Base><!--Base[5]-->
                <BaseFilePath>C:/Image</BaseFilePath>
                <BaseFileName>icon.png</BaseFileName>
              </Base>
            </Frame_Element>
          </Frame_Elements>
        </FRAME>

    </CONFIG>

    If I want to get <Id>5

    <Frame_Element><!--Frame_Element[5]-->
              <Basic><!--Basic[5]-->
                <Id>5</Id>

    How to get Id value

    And If I want to modify FrameWidth to 100

    And modify any <Basic> or <Base> item .

    How to do those?

    Thanks



    • Edited by CY_Chen Friday, September 27, 2019 2:07 AM
    Thursday, September 26, 2019 8:43 AM

All replies

  • Hi CY_Chen,

    Thank you for posting here.

    For your question, you want to do some operation for XML such as getting the value, adding new node and modifying some Element. 

    You could try the following code to get it. 

    class Program
        {
            static void Main(string[] args)
            {
                string startupPath = Environment.CurrentDirectory;
                String projectRootPath = Directory.GetParent(startupPath).Parent.FullName;
                String filePath = projectRootPath + @"\" + "XMLFile1.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                // Get the value
                XmlNodeList list = doc.SelectNodes(".//FRAME/Frame_Elements/Frame_Element");
                XmlNode node = list[list.Count - 1];
                XmlNode id =  node.SelectSingleNode("Basic").SelectSingleNode("Id");
                Console.WriteLine(id.Value);
              
                // Add new XMLNode
                AppendNewFrame(doc,filePath);
                
                // Get All Base
                XmlNodeList bases = doc.SelectNodes(".//FRAME/Frame_Elements/Frame_Element/Base");
               
                // Now you have all Base,because the element Base has no unique identifier
                // you have to modify them by index
                XmlNode baseFilePath = bases[bases.Count - 1].SelectSingleNode("BaseFilePath");
                baseFilePath.InnerText = "d:/BaseFilePath";
                doc.Save(filePath);
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();  
            }
            static void AppendNewFrame(XmlDocument doc,String filePath)
            {
                XmlNode rootNode = doc.DocumentElement;
                XmlNode frames = rootNode.SelectSingleNode("Frames");
                XmlNode newFrame = doc.CreateElement("FRAME");
                XmlAttribute xmlAttribute = doc.CreateAttribute("FrameName");
                xmlAttribute.Value = "Frame2";
                newFrame.Attributes.Append(xmlAttribute);
    
                XmlNode newFrame_Elements = doc.CreateElement("Frame_Elements");
    
                XmlNode newFrame_Element = doc.CreateElement("Frame_Element");
    
                XmlNode newBasic = doc.CreateElement("Basic");
                XmlElement newId = doc.CreateElement("Id");
                newId.InnerText = "6";
                XmlElement newUid = doc.CreateElement("Uid");
                newUid.InnerText = "0";
                newBasic.AppendChild(newId);
                newBasic.AppendChild(newUid);
    
                XmlNode newBase = doc.CreateElement("Base");
    
                XmlElement newBaseFilePath = doc.CreateElement("BaseFilePath");
                newBaseFilePath.InnerText = "c:/a";
                XmlElement newBaseFileName = doc.CreateElement("BaseFileName");
                newBaseFileName.InnerText = "a.jpg";
                newBase.AppendChild(newBaseFilePath);
                newBase.AppendChild(newBaseFileName);
    
                newFrame_Element.AppendChild(newBasic);
                newFrame_Element.AppendChild(newBase);
                newFrame_Elements.AppendChild(newFrame_Element);
                newFrame.AppendChild(newFrame_Elements);
                frames.AppendChild(newFrame);
    
                doc.Save(filePath);
            }
    }

    Hope my solution could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.





    Friday, September 27, 2019 6:26 AM
    Moderator
  • Hi Chen : The below is similar to what Jack answered ..Added code to get/set framewidth

    Hope this helps..

           var filename = @"c:\Chen\config.xml";
                XDocument objDoc = XDocument.Load(filename);
                if (objDoc.Element("CONFIG").Attribute("FrameWidth") != null)
                {
                    //Read FrameWidth
                    var framewidth = objDoc.Element("CONFIG").Attribute("FrameWidth").Value;
                    //Set FrameWidth
                    objDoc.Element("CONFIG").Attribute("FrameWidth").Value = "500";
                    objDoc.Save(filename);
                }
               //
                objDoc = XDocument.Load(filename); 
                foreach(XElement el in  objDoc.Descendants("Basic"))
                {
                     //if()Filter for any condition
                    Console.Write("ID = {0} UID = {1} ", el.Descendants("Id").FirstOrDefault().Value, el.Descendants("Uid").FirstOrDefault().Value);
                    //you can set and save  el.Descendants("Id").Value = your value; objDoc.Save
                }

    Friday, September 27, 2019 9:43 PM