locked
The <SQL> tag is being removed when I update the Inner Text RRS feed

  • Question

  • User-1188570427 posted

    I am trying to go into a specific node and update the <Sql> tag.

    I can update the inner text, but then it is leaving off the <Sql> tag which is then causing the Query to not be there.

    What am I doing wrong?

      foreach (XmlNode item in nodes)
                            {
                                if (item != null)
                                {
                                    if (item.FirstChild != null)
                                    {
                                        if (string.IsNullOrWhiteSpace(item.FirstChild.Name) == false)
                                        {
                                            if (item.FirstChild.Name == "Sql")
                                            {
                                                item.InnerText = await serviceQueryFilter.InjectOrganizationFilter(userId, Id, item.InnerText).ConfigureAwait(false);
    
                                                item.InnerText = "<Sql>" + item.InnerText + "</Sql>"; // This is wrong, but after updating the text how do I re apply the <Sql></Sql>
                                            }
                                        }
                                    }
                                }
                            }

    Thursday, May 10, 2018 9:25 PM

Answers

  • User-1188570427 posted

    Here is the code I used to update the xml with the new value of the <Sql> Node:

                    XElement xml = XElement.Parse(xml);
    
                    foreach (var item in xml.Descendants("Query"))
                    {
                        if (item != null)
                        {
                            if (item.Element("Sql") != null)
                            {
                                if (string.IsNullOrWhiteSpace(item.Element("Sql").Value) == false)
                                {
                                    string currentValue = item.Element("Sql").Value;
                                    item.Element("Sql").Value = await service.UpdateXml(userId, groupId, currentValue).ConfigureAwait(false);
                                }
                            }
                        }
                    }
    
                    StringWriter sw = new StringWriter();
                    XmlTextWriter tx = new XmlTextWriter(sw);
                    xml.WriteTo(tx);
    
                    xml = sw.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 18, 2018 2:38 PM

All replies

  • User36583972 posted


    Hi tvb2727,

    I can update the inner text, but then it is leaving off the <Sql> tag which is then causing the Query to not be there.

    We can update the inner text and can be queried again. But I cannot understand the following code you have used.

        item.InnerText = "<Sql>" + item.InnerText + "</Sql>"; // This is wrong, but after updating the text how do I re apply the <Sql></Sql>

    Modifying Nodes, Content, and Values in an XML Document
    https://docs.microsoft.com/en-us/dotnet/standard/data/xml/modifying-nodes-content-and-values-in-an-xml-document

    Please include all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages or you can upload your demo that We can download it and debugging. This will help us quickly analyze your problem.


    Best Regards,

    Yong Lu

    Monday, May 14, 2018 2:21 AM
  • User-1188570427 posted

    Here is the code I used to update the xml with the new value of the <Sql> Node:

                    XElement xml = XElement.Parse(xml);
    
                    foreach (var item in xml.Descendants("Query"))
                    {
                        if (item != null)
                        {
                            if (item.Element("Sql") != null)
                            {
                                if (string.IsNullOrWhiteSpace(item.Element("Sql").Value) == false)
                                {
                                    string currentValue = item.Element("Sql").Value;
                                    item.Element("Sql").Value = await service.UpdateXml(userId, groupId, currentValue).ConfigureAwait(false);
                                }
                            }
                        }
                    }
    
                    StringWriter sw = new StringWriter();
                    XmlTextWriter tx = new XmlTextWriter(sw);
                    xml.WriteTo(tx);
    
                    xml = sw.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 18, 2018 2:38 PM