-1

I am new to managing XML, I've read a couple of articles but I'm confused when it comes to a specific XML that I am working on. Can someone help me with the right statement? I just want to update the value of the ListStart, but I am getting an error when compiling. I am updating that part by this:

        XmlDocument soapEnvelopeDocument = new XmlDocument();
        soapEnvelopeDocument.Load(@"path");
        XmlNode myNode = soapEnvelopeDocument.SelectSingleNode("descendant::cet:GetListCustomElement[cet:GetListCustom/cet:ListID='101']");
        soapEnvelopeDocument.LastChild.InnerText = sDate;
  <soapenv:Header/>
  <soapenv:Body>
    <cet:GetListCustomElement>
      <!--Zero or more repetitions:-->
      <cet:GetListCustom>
        <cet:ListID>101</cet:ListID>
        <cet:ListStart>13.11.2020</cet:ListStart>
      </cet:GetListCustom>
    </cet:GetListCustomElement>
  </soapenv:Body>
</soapenv:Envelope>```
Deegee
  • 91
  • 1
  • 5
  • What is the error you get? – Oguz Ozgul Nov 13 '20 at 18:33
  • @OguzOzgul System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.' – Deegee Nov 13 '20 at 18:36
  • You need to use an `XmlNamespaceManager`. In fact this looks like a duplicate of [SelectSingleNode returns null when tag contains xmlNamespace](https://stackoverflow.com/q/4171451/3744182), agree? [Xml-SelectNodes with default-namespace via XmlNamespaceManager not working as expected](https://stackoverflow.com/a/4271875/3744182) and [Using Xpath With Default Namespace in C#](https://stackoverflow.com/q/585812/3744182) could help as well. – dbc Nov 13 '20 at 20:03
  • Actually, your XML is malformed. If I try to parse your XML I get `System.Xml.XmlException: 'soapenv' is an undeclared prefix. Line 1, position 2.`. Also, you never use `myNode` after having selected it. Did you mean to do `myNode.LastChild.InnerText = sDate;` instead of `soapEnvelopeDocument.LastChild.InnerText = sDate;`? Demo fiddle of your XML generating parse errors here: https://dotnetfiddle.net/RlaO8C If you can [edit] your question to provide a [mcve] we can check to see if those answers work. Absent a [mcve] your question will probably be closed as a duplicate. See: [ask]. – dbc Nov 13 '20 at 20:38
  • Already edited my post @dbc – Deegee Nov 16 '20 at 05:49

1 Answers1

0

You only supplied a piece of the xml without the namespaces. With Xml linq you can get the element without the namespaces. See code below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement ListStart = doc.Descendants().Where(x => x.Name.LocalName == "ListStart").FirstOrDefault();

            ListStart.SetValue("14.11.2020");
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20