none
XML Data Update RRS feed

  • Question

  • Hello everybody,

    I have an xml file which contains datas. And I want to update some nodes which are connected to "gcisim", "kartno" and "kanalno" are  from C#. Here is my XML codes:

    <?xml version="1.0" encoding="utf-8" ?>
    <root>
      <gctipi>
        <gcisim>AG</gcisim>
        <gckart>
          <kartno>1</kartno>
          <gc>
            <sira>1</sira>
            <kanalno>1</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>0</bos>
          </gc>
          <gc>
            <sira>2</sira>
            <kanalno>2</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>0</bos>
          </gc>
          <gc>
            <sira>3</sira>
            <kanalno>3</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>0</bos>
          </gc>
        </gckart>
      </gctipi>
      <gctipi>
        <gcisim>SG</gcisim>
        <gckart>
          <kartno>1</kartno>
          <gc>
            <sira>1</sira>
            <kanalno>1</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>1</bos>
          </gc>
          <gc>
            <sira>2</sira>
            <kanalno>2</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>1</bos>
          </gc>
          <gc>
            <sira>3</sira>
            <kanalno>3</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>1</bos>
          </gc>
        </gckart>
        <gckart>
          <kartno>2</kartno>
          <gc>
            <sira>1</sira>
            <kanalno>1</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>1</bos>
          </gc>
          <gc>
            <sira>2</sira>
            <kanalno>2</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>1</bos>
          </gc>
          <gc>
            <sira>3</sira>
            <kanalno>3</kanalno>
            <isim></isim>
            <aktif>0</aktif>
            <bos>1</bos>
          </gc>
        </gckart>
      </gctipi>
    </root>

    And  C# Codes:

    try
                {
                    XmlDocument xml = new XmlDocument();
                    xml.Load("XMLDosyalari/XMLGC.xml");            
    
                    foreach (XmlElement element in xml.SelectNodes("//gctipi"))
                    {
                        foreach (XmlElement element1 in element)
                        {
                            if (element.SelectSingleNode("//gcisim").Value == gci)
                            {
                                foreach (XmlElement element2 in element1)
                                {
                                    if (element.SelectSingleNode("//kartno").Value == comboBoxKartNo.Text)
                                    {
                                        if (element.SelectSingleNode("//kanalno").InnerText == comboBoxKanalNo.Text)
                                        {
                                           
                                           element.SelectSingleNode("//isim").InnerText = textBoxIsim.Text;
    
                                            if (checkBoxAktif.Checked)
                                                element.SelectSingleNode("//aktif").Value = "1";
                                            else
                                                element.SelectSingleNode("//aktif").Value = "0";
    
                                            if (comboBoxGirisTuru.SelectedIndex > 4)
                                            {
                                                if (comboBoxGirisTuru.SelectedIndex == 0)
                                                    element.SelectSingleNode("//butontur").Value = "1";
                                                else
                                                    element.SelectSingleNode("//butontur").Value = "0";
                                            }
    
                                            element.SelectSingleNode("//bos").InnerText = "0";
    
                                            xml.Save("0.xml");
    
                                            break;
                                        }                                   
                                    }            
                                }
                            }
                        }
                    }
                    Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Hata");
                }
    Thanks for your helps.

    Tuesday, August 14, 2018 6:40 AM

Answers

  • Here is my solution thanks to Sabah Shariq and Vivek Bansod.

    XmlDocument xml = new XmlDocument();
                    xml.Load(yol);
                    bool gc = false;
                    XmlNodeList gctipi = xml.SelectNodes("//gctipi");
                    XmlNodeList gckart = xml.SelectNodes("//gckart");
    
                    foreach (XmlNode node in gctipi)
                    {
                        if (node.SelectSingleNode(@".//gcisim").InnerText == "AG")
                        {
                            gc = true;
                            foreach (XmlNode node2 in gckart)
                            {
                                if (node2.SelectSingleNode(@".//kartno").InnerText == "2")
                                {
                                    if (node2.SelectSingleNode(@".//kanalno").InnerText == "1")
                                    {
                                        if (gc)
                                        {
                                            node2.SelectSingleNode(@".//isim").InnerText = textBoxIsim.Text;
                                            node2.SelectSingleNode(@".//aktif").InnerText = "newValueInnerText";
                                            node2.SelectSingleNode(@".//bos").InnerText = "newValueInnerText";
                                            gc = false;
                                        }
                                    }
                                }
                            }
                        }
                    }  
                    xml.Save("0.xml");       

    • Marked as answer by Ali Dayan Wednesday, August 15, 2018 11:00 AM
    Wednesday, August 15, 2018 11:00 AM

All replies

  • Hi,

    Please below line of code. you don't have to have these many foreach loops.

    XmlDocument xml = new XmlDocument();
    xml.Load("test.xml");
    
    XmlNodeList gctipi = xml.SelectNodes("//gctipi");
    
    foreach (XmlNode node in gctipi)
    {
                        
       node.SelectSingleNode(@".//gcisim").InnerText = "newValue";
                        
       node.SelectSingleNode(@".//kartno").InnerText = "newValue";
                        
       node.SelectSingleNode(@".//kanalno").InnerText = "newValue";
    }
    
    xml.Save("test.xml");   


    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Tuesday, August 14, 2018 10:48 AM
  • Thank you for your answer.

    I want to do something like:

    I want to change values of isim, aktif and bos where gcisim = "AC" and kartno = "1" and kanalno = "2".



    • Edited by Ali Dayan Tuesday, August 14, 2018 2:09 PM
    Tuesday, August 14, 2018 1:25 PM
  • Ali,

    Try the below line of code. Modify it as per your requirement. We are not here to do all the work for you. We can guide you then show us where you get stuck and we can help you from there. Please try by yourself first.

    if(node.SelectSingleNode(@".//gcisim").InnerText == "AC")
                    {
                        if(node.SelectSingleNode(@".//kartno").InnerText == "1")
                        {
                            node.SelectSingleNode(@".//isim").InnerText = "newValueInnerText";
                            node.SelectSingleNode(@".//aktif").InnerText = "newValueInnerText";
                            node.SelectSingleNode(@".//bos").InnerText = "newValueInnerText";
                        }
                    }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Tuesday, August 14, 2018 2:27 PM
    Moderator
  • Yes you are right you are not here to do all the work for you. But that's not working and I have stucked there. I have tried hundred of methods including your solution and nothing works.
    Wednesday, August 15, 2018 9:54 AM
  • Here is my solution thanks to Sabah Shariq and Vivek Bansod.

    XmlDocument xml = new XmlDocument();
                    xml.Load(yol);
                    bool gc = false;
                    XmlNodeList gctipi = xml.SelectNodes("//gctipi");
                    XmlNodeList gckart = xml.SelectNodes("//gckart");
    
                    foreach (XmlNode node in gctipi)
                    {
                        if (node.SelectSingleNode(@".//gcisim").InnerText == "AG")
                        {
                            gc = true;
                            foreach (XmlNode node2 in gckart)
                            {
                                if (node2.SelectSingleNode(@".//kartno").InnerText == "2")
                                {
                                    if (node2.SelectSingleNode(@".//kanalno").InnerText == "1")
                                    {
                                        if (gc)
                                        {
                                            node2.SelectSingleNode(@".//isim").InnerText = textBoxIsim.Text;
                                            node2.SelectSingleNode(@".//aktif").InnerText = "newValueInnerText";
                                            node2.SelectSingleNode(@".//bos").InnerText = "newValueInnerText";
                                            gc = false;
                                        }
                                    }
                                }
                            }
                        }
                    }  
                    xml.Save("0.xml");       

    • Marked as answer by Ali Dayan Wednesday, August 15, 2018 11:00 AM
    Wednesday, August 15, 2018 11:00 AM