-1

I'm trying to change a the value for the model atribute from the following .xml file.

<MTConnectDevices xmlns="urn:mtconnect.org:MTConnectDevices:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1 http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
  <Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="2" bufferSize="131072" />
  <Devices>
    <Device name="XX" iso841Class="1" uuid="000" id="id1001">
      <Description manufacturer="name" serialNumber="Serial Number" model="UNDEFINED">
      </Description>

This is what I've tried but it's not working as it's not changing the value for model.

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(mypath);
        var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
        var node = xmlDoc.SelectSingleNode("/ns:MTConnectDevices/ns:Header/ns:Devices/ns:Device/ns:Description", nsmgr);
        node.Attributes["model"].Value = "changed";
        xmlDoc.Save(mypath);

Can anyone give me a hand and tell me how to fix this?

Avión
  • 7,963
  • 11
  • 64
  • 105
  • In what way isn't it working? – David Arno May 20 '15 at 06:46
  • It's not changing the value. – Avión May 20 '15 at 06:47
  • Have you inspected the state of `node` in the debugger, or is it just that the value in the file isn't being updated? Does changing `/MTConnectDevices` to `//MTConnectDevices` make any difference? – David Arno May 20 '15 at 06:53
  • On the `node.Attributes["model"].Value = "newvalue";` Im getting a `NullReferenceException` . This means it's not fetching the node correctly? – Avión May 20 '15 at 07:14
  • Are you trying to change the value of the MTConnect stream itself? If so, that defeats the purpose of MTConnect. Regardless, if you're still interested in MTConnect, I'd recommend following http://area51.stackexchange.com/proposals/80667/internet-of-things so there's a better place to post questions about MTConnect. – tbm0115 Nov 05 '15 at 15:39

1 Answers1

5

Your xml contains namespaces, so you have to use them while retrieving node.

It can be done something like this:

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
var node = xmlDoc.SelectSingleNode("/ns:MTConnectDevices/ns:Devices/ns:Device/ns:Description", nsmgr);
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • When I try to change the node using `node.Attributes["model"].Value = "newvalue";` im getting a `NullReferenceException` . This means it's not fetching the node correctly? – Avión May 20 '15 at 07:16
  • @Borja This means either node can't be found by specified path (so it is null) or node found, but it has no `model` attribute. Have you provided relevant xml? I'm not getting any exceptions on xml you've provided (using modified path including Header), and model attribute is being updated. – Andrey Korneyev May 20 '15 at 07:27
  • I know where's the problem. It's on the URL's from the `MTConnectDevices` atributes: `` Testing, if I remove the URL's and leave only the `test` attribute everything works fine and the `test` attribute changes perfectly. `` Any ideas of what's happening? – Avión May 20 '15 at 07:43
  • 1
    @Borja See update. The fact that your xml has namespaces is essential, it was better not to omit this fact in question. – Andrey Korneyev May 20 '15 at 08:03
  • Thanks for the update Andy. Now I'm having the same `NullReferenceException` on the `nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);` value. Any ideas? – Avión May 20 '15 at 08:07
  • @Borja Can you use the debugger to determine what exactly null in this line? is it xmlDoc, is it xmlDoc.DocumentElement or anything else? Where exactly have you placed this code (I hope, after loading document to xmlDoc)? Update your question with relevant xml and code, otherwise it's hard to say what's wrong on your side. – Andrey Korneyev May 20 '15 at 08:11
  • Ignore the last comment. After copying and pasting I removed the `xmlDoc.Load(mypath);` line unintentionally so it was not loading correctly. Anyways, I'm still getting the same error `NullReferenceException` on the same line `node.Attributes["model"].Value = "changed";` . I upadted the solution with the code and a proper version of the `.xml`. Thanks in advance. Your help is much appreciated. – Avión May 20 '15 at 08:18
  • Solved! I fixed it by removing the `Header` from the path `var node = xmlDoc.SelectSingleNode("/ns:MTConnectDevices/ns:Devices/ns:Device/ns:Description", nsmgr);` Please, update your answer so I can accept it. Cheers! – Avión May 20 '15 at 08:22
  • 1
    @Borja oh... seeing your **actual** xml it's clearly evident that porblem was only in namespaces since `Header` tag is self-closed. I will update my answer and remove misleading part, but please - post relevant information to avoid confusion.... – Andrey Korneyev May 20 '15 at 08:26
  • Will do next time. Thanks a lot for your help, Andy. – Avión May 20 '15 at 08:27