none
xml in C# RRS feed

  • Question

  • hello,i have xml file :

    <?xml version="1.0"?>
    <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>

    how to read/write from/to specific line in xml file in C#????



    Monday, June 11, 2018 9:51 AM

Answers

  • how to read/write from/to specific line in xml file in C#????

    http://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples

    https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b


    • Marked as answer by sami kh Monday, June 11, 2018 10:26 AM
    • Unmarked as answer by sami kh Monday, June 11, 2018 10:31 AM
    • Marked as answer by sami kh Monday, June 11, 2018 10:31 AM
    Monday, June 11, 2018 10:21 AM
  • How is that line specified, by a certain value or by its position in the XML string? Generally you could use the tools of the XDocument class to query your string, for example :
    string xml = @"<?xml version='1.0'?>
    <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>";
    
    XDocument doc = XDocument.Parse(xml);
    
    var result = doc.Descendants("title").ToList();
    
    Console.WriteLine($"{result[0].Value}");
    
    
    wizend
    • Marked as answer by sami kh Monday, June 11, 2018 10:44 AM
    Monday, June 11, 2018 10:27 AM
  • by its position

    • Marked as answer by sami kh Tuesday, June 12, 2018 10:08 AM
    Monday, June 11, 2018 10:32 AM
  • i write the code:s

     ;("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 ????a

    • Marked as answer by sami kh Tuesday, June 12, 2018 10:08 AM
    Monday, June 11, 2018 12:46 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");

    Best 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 8:21 AM
    Tuesday, June 12, 2018 6:54 AM
  • NO,I want to write the value in the same file
    • Marked as answer by sami kh Tuesday, June 12, 2018 10:08 AM
    Tuesday, June 12, 2018 8:22 AM
  • Hi sami,

    You can try 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");
            }

    Result:

    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.

    • Marked as answer by sami kh Thursday, June 28, 2018 12:54 PM
    Monday, June 25, 2018 2:16 AM

All replies

  • how to read/write from/to specific line in xml file in C#????

    http://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples

    https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b


    • Marked as answer by sami kh Monday, June 11, 2018 10:26 AM
    • Unmarked as answer by sami kh Monday, June 11, 2018 10:31 AM
    • Marked as answer by sami kh Monday, June 11, 2018 10:31 AM
    Monday, June 11, 2018 10:21 AM
  • How is that line specified, by a certain value or by its position in the XML string? Generally you could use the tools of the XDocument class to query your string, for example :
    string xml = @"<?xml version='1.0'?>
    <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>";
    
    XDocument doc = XDocument.Parse(xml);
    
    var result = doc.Descendants("title").ToList();
    
    Console.WriteLine($"{result[0].Value}");
    
    
    wizend
    • Marked as answer by sami kh Monday, June 11, 2018 10:44 AM
    Monday, June 11, 2018 10:27 AM
  • by its position

    • Marked as answer by sami kh Tuesday, June 12, 2018 10:08 AM
    Monday, June 11, 2018 10:32 AM
  • i write the code:s

     ;("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 ????a

    • Marked as answer by sami kh Tuesday, June 12, 2018 10:08 AM
    Monday, June 11, 2018 12:46 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");

    Best 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 8:21 AM
    Tuesday, June 12, 2018 6:54 AM
  • NO,I want to write the value in the same file
    • Marked as answer by sami kh Tuesday, June 12, 2018 10:08 AM
    Tuesday, June 12, 2018 8:22 AM
  • Hi Dolen,

    I want to write the read value in the same file but in another node.

     I mean i do not want to overwrite the file but I want to change the node data

    please help me with that.


    • Edited by sami kh Sunday, June 24, 2018 7:53 AM
    Sunday, June 24, 2018 7:36 AM
  • Hi Wizend,

    thank you for help me to read specific node,but now how to write the read value in the same file but in another node??

    I mean i do not want to overwrite the file but I want to change the node data.

    • Edited by sami kh Sunday, June 24, 2018 7:54 AM
    Sunday, June 24, 2018 7:41 AM
  • Hi sami,

    You can try 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");
            }

    Result:

    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.

    • Marked as answer by sami kh Thursday, June 28, 2018 12:54 PM
    Monday, June 25, 2018 2:16 AM
  • If you want to write the value into a different, already existent node, then you can use the XElement.ReplaceWith method. If you want to add the value to your file within a additional new node, you can use the XElement.Add method. (But check your original xml string. There are a lot of leading and trailing whitespaces that doesn't belong there.)
    string xml = @"<?xml version='1.0'?>
    <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>";
    
    XElement doc = XElement.Parse(xml);
    
    var element = doc.Elements("title").Where(v => v.Value.StartsWith("OUT[2]")).First();
    element.ReplaceWith(new XElement("title", "IN[3] = true"));
    
    Console.WriteLine(doc);
    
    doc.Add(new XElement("title", "OUT[5] = false"));
    //doc.Save("..\\..\\xmlFile.xml");
    Console.WriteLine(doc);
    
    
    wizend
    • Marked as answer by sami kh Thursday, June 28, 2018 12:54 PM
    • Unmarked as answer by sami kh Thursday, June 28, 2018 12:54 PM
    Monday, June 25, 2018 9:24 AM
  • thank you Dolen,

    Using your code you were able to change the value but a copy of the file was created and saved and the original was not modified.

    question1: Is it possible to edit without creating a copy and editing it?
    question 2:How is the value read continuously so that when modified, the same value is modified?
    I mean that the read value is stored in the listbox and displayed when modified. I want the value to be shown after the modification in the listbox,how do that??

    Thursday, June 28, 2018 12:48 PM