0

I am trying to edit the values of an xml document following the instructions found on another post here How to modify existing XML file with XmlDocument and XmlNode in C# .

here is my code

XmlDocument xml = new XmlDocument();
        xml = xml.Load(@"https://www.aade.gr/sites/default/files/2020-09/SampleXML_1.1%20%20%28%CE%A4%CE%99%CE%9C-%CE%A0%CE%A9%CE%9B%CE%97%CE%A3%CE%97%CE%A3_%CE%91%CE%A5%CE%A4%CE%9F%CE%A4%CE%99%CE%9C%29%20.xml");

        XmlNodeList aNodes = xml.SelectNodes("/InvoicesDoc/invoice/issuer/vatNumber");


        foreach (XmlNode aNode in aNodes)
        {
            XmlAttribute vatAttribute = aNode.Attributes["vatNumber"];
            vatAttribute.Value = "123456789";
            
        }

        xml.Save(@"C:\Users\Kostas\Desktop\mydata\infinal.xml");

My problem is that XmlNodeList aNodes will return empty; i have tried to change the xml.SelectNodes("/InvoicesDoc/invoice/issuer/vatNumber") to xml.SelectNodes("/InvoicesDoc/invoice/issuer") and all the way up to single xml.SelectNodes("/InvoicesDoc") but still XmlNodeList aNodes will return empty. First attempts i loaded the XML doc from file and had the issue. Then i thought maybe something wrong with the file so changed the load of the file directly from the site provides this xml template i need to work on. Both options will load the file fine as i can see it when is saved but my changes will not complete since aNodes is empty and foreach loop will skip straight away.

What am i doing wrong?

thanks for your help in advance.

ps this is the xml i need to edit https://www.aade.gr/sites/default/files/2020-09/SampleXML_1.1%20%20%28%CE%A4%CE%99%CE%9C-%CE%A0%CE%A9%CE%9B%CE%97%CE%A3%CE%97%CE%A3_%CE%91%CE%A5%CE%A4%CE%9F%CE%A4%CE%99%CE%9C%29%20.xml

Update: I just tried with another xlm found in microsoft example called books on https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85) XmlNodelist will also return null/empty when i look for /catalog/book . So the good side is that there is no problem with original xml file i need to edit and the bad side is that still i cannot figure out what i am doing wrong.

Kostas L
  • 5
  • 5
  • Have you tried aNode.Attributes[“vatNumber”].Value = “123456789”; – Vivek Nuna Oct 09 '21 at 07:44
  • The foreach loop will skip straight away and will not proceed to its code as aNodes is empty. this is my problem. The **XmlAttribute vatAttribute=aNode.Attributes["vatNumber"]; vatAttribute.Value = "123456789"; ** will not be executed at all as foreach loop skips in the very first iteration as the aNodes is empty there is nothing to iterate throu. – Kostas L Oct 09 '21 at 07:59
  • can you please add the xml in proper formatted way in this question only – Vivek Nuna Oct 09 '21 at 08:01
  • ` 999999999 GR 1 888888888 GR 0
    ` hope this is a proper way but all xml will not fit in
    – Kostas L Oct 09 '21 at 08:07

1 Answers1

0

XmlNodeList aNodes returns null because the xml contains these namespace declarations:

<InvoicesDoc xmlns=\"http://www.aade.gr/myDATA/invoice/v1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
xsi:schemaLocation=\"http://www.aade.gr/myDATA/invoice/v1.0/InvoicesDoc-v0.6.xsd\" 
xmlns:icls=\"https://www.aade.gr/myDATA/incomeClassificaton/v1.0\" 
xmlns:ecls=\"https://www.aade.gr/myDATA/expensesClassificaton/v1.0\">

You need to manage your xml doing something like this:

XmlDocument xml = new XmlDocument();
xml.Load(@"https://www.aade.gr/sites/default/files/2020-09/SampleXML_1.1%20%20%28%CE%A4%CE%99%CE%9C-%CE%A0%CE%A9%CE%9B%CE%97%CE%A3%CE%97%CE%A3_%CE%91%CE%A5%CE%A4%CE%9F%CE%A4%CE%99%CE%9C%29%20.xml");

XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
            manager.AddNamespace("InvoicesDoc", "http://www.aade.gr/myDATA/invoice/v1.0");

//Example to get the root element
XmlNodeList root = xml.SelectNodes("/InvoicesDoc:InvoicesDoc", manager);

//Example to get the VatNumber tag
XmlNodeList aNodes =xml.SelectNodes("/InvoicesDoc:InvoicesDoc/InvoicesDoc:invoice/InvoicesDoc:issuer/InvoicesDoc:vatNumber", manager);
RXolio
  • 134
  • 3
  • tried but same issue. thanks for your post. well i tried with xdocument.loadxml and i get an error when trying to load the xml with message data at the root level is invalid.Line 1 position 1. can this be the issue? i dont know am still on research as it may be a special character in start of xml causing the issue according to error i get from xlmLoad – Kostas L Oct 09 '21 at 10:26
  • Well....As i told you it did not work when i first tried.While i was trying to find solution i deleted the xml file i was loading from disk.Then i did run my code only with 2 lines.One was to load the xml doc from providers website and the second line was to save the xml file on disk. then i changed the load of xml from the file on disk i had just saved using my code. Then your code worked fine and the aNodes did not return empty!actually it did load only 1 node that had all info inside.Now i am debating, go get a shepherd job or keep trying to get it right? Thank you very much for you help – Kostas L Oct 09 '21 at 13:04
  • 2 hours am still trying to get the rest of the nodes with no luck .any extra help will be very appreciated. – Kostas L Oct 09 '21 at 15:26
  • I have modified my answer adding the way to read the tag that you needed, when you work with xml files that contains namespaces you need to add the namespace for each element on the xpath. – RXolio Oct 11 '21 at 04:52
  • You are top! thank you... i will have that in mind for future use as now i found another work around which works fine with the specific xml file as it does not have a lot of duplicated nodes.So i end up using doc.GetElementsBytagName then check the parent node and act accordingly.But you tought me how to edit xml and thank you very much for that. here is my code now : XmlNodeList ele2 = doc.GetElementsByTagName("vatNumber"); foreach (XmlNode n in ele2) { if (n.ParentNode.Name == "counterpart") { – Kostas L Oct 11 '21 at 06:21