none
xml in C# RRS feed

  • Question

  • hello everyone!

    i write the code:

     XDocument doc = XDocument.Load("C:\\Users\\Sami\\Desktop\\sami11.xml");
     var result = doc.Descendants("title").ToList();
                (listBox1.Items.Add(result[1].Value);

    how to write the value "result[1].value" in xml file ????

    Monday, June 11, 2018 12:48 PM

All replies

  • I'm not sure I follow your requirement. result is a list of XElement items. It would already be in the XML file so you won't need to write it again. Please clarify what you are trying to do in the above code, what you want to accomplish in general and a sample XML file to look at.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by sami kh Tuesday, June 12, 2018 7:20 AM
    • Unmarked as answer by sami kh Saturday, June 16, 2018 12:08 PM
    Monday, June 11, 2018 2:00 PM
    Moderator
  • If you want to add another element to the xml string of your earlier post take a look at the XElement.Add method:

    doc.Element("sami").Add(new XElement("title", "OUT[5] = false"));
    
    wizend

    • Marked as answer by sami kh Tuesday, June 12, 2018 7:23 AM
    • Unmarked as answer by sami kh Saturday, June 16, 2018 12:08 PM
    Monday, June 11, 2018 7:20 PM
  • Hi sami, 

    I think that You need save the file using a StreamWriter object.

    Look above a method that I wrote to solve this scenary:

            public void GravarArquivoXML(String NomeArquivo, XmlDocument XML)
            {
                StreamWriter twArqSaida = null;
    
                try
                {
                    if (!System.IO.Directory.Exists(Path.GetDirectoryName(NomeArquivo)))
                        System.IO.Directory.CreateDirectory(Path.GetDirectoryName(NomeArquivo));
    
                    // Gravar saída...                
                    twArqSaida = new StreamWriter(NomeArquivo);
                    twArqSaida.Write(XML.OuterXml);
                    twArqSaida.Close();
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("Erro ao criar o arquivo: {0}. Msg: {1}", NomeArquivo, ex.Message), ex);
                }
                finally
                {
                    if (twArqSaida != null)
                        twArqSaida.Dispose();
                }
            }

    Then, You can load the document as You did, and You can put the new tag, using a part of code, something like wizend wrote previously:

    doc.Element("sami").Add(new XElement("title", "OUT[5] = false"));


    And saving invoking the method. Example:

    Object.GravarArquivoXML(String.Format(@"C:\Temp\File.xml", doc);

    Regards.

    PS: I'm sorry if my english isn't so good. My native language is Brazilian Portuguese!

    Anderson.


    • Edited by Anderson H. Meneghesso Monday, June 11, 2018 9:33 PM
    • Marked as answer by sami kh Tuesday, June 12, 2018 7:26 AM
    • Unmarked as answer by sami kh Saturday, June 16, 2018 12:08 PM
    Monday, June 11, 2018 9:32 PM
  • Hi sami,

    Are you saying that you want to write the value "result[1].value" in a new xml file? If yes, you can check this:

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("test.xml");
                XmlNodeList result = xmlDoc.SelectNodes("//title");
                XmlDocument xmlDocNew = new XmlDocument();
                XmlElement newElement = xmlDocNew.CreateElement("title");
                newElement.InnerText = result[1].InnerText;
                xmlDocNew.AppendChild(newElement);
                xmlDocNew.Save("test2.xml");

    Regards,

    Dolen


    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.

    • Marked as answer by sami kh Tuesday, June 12, 2018 7:26 AM
    • Unmarked as answer by sami kh Saturday, June 16, 2018 12:08 PM
    Tuesday, June 12, 2018 7:01 AM
  • i trying read a value spesific from xml file and write  this value in the same xml file

    • Marked as answer by sami kh Tuesday, June 12, 2018 7:30 AM
    • Unmarked as answer by sami kh Saturday, June 16, 2018 12:09 PM
    Tuesday, June 12, 2018 7:23 AM
  • NO,I want to write the value in the same file
    • Marked as answer by sami kh Tuesday, June 12, 2018 7:30 AM
    • Unmarked as answer by sami kh Saturday, June 16, 2018 12:09 PM
    Tuesday, June 12, 2018 7:28 AM
  • Hi sami,

    Please check this.

     static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("test.xml");
                XmlNodeList result = doc.SelectNodes("//title");
                //write the value of result[1]
                result[1].InnerText = " IN[2] = false";
                //save the change to the existing file.
                doc.Save("test.xml");
            }
    Original xml file:
    <sami>
      <title> IN[1] = true </title>
      <title> IN[2] = true </title>
      <title> OUT[1] = true </title>
      <title> OUT[2] = flase </title>
      <title> OUT[3] = flase </title>
      <title> OUT[4] = flase </title>
      <title>$out[55]=true</title> 
    </sami>
    
    After writing the value "result[1].value" in the same xml file:
    <sami>
      <title> IN[1] = true </title>
      <title> IN[2] = false</title>
      <title> OUT[1] = true </title>
      <title> OUT[2] = flase </title>
      <title> OUT[3] = flase </title>
      <title> OUT[4] = flase </title>
      <title>$out[55]=true</title>
    </sami>
    
    Regards,

    Dolen


    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.

    Thursday, June 21, 2018 9:00 AM