1

Normally XML elements to get edited on web page are sent over as the input name Example :

Then In C# controller method:

List<string> keys = Request.Form.Keys.Cast<string>().Where(item => item[0] == '/' && item[1] == '/').ToList();
foreach (var key in keys)
{
   var item = xDoc.SelectSingleNode(key, manager);
   if (item != null)
      item.InnerText = Request.Form[key];
   else
       throw new InvalidOperationException("Could not decode parameter with key " + key + " and value " + Request.Form[key]);
}

Normally the XML elements are sent over in a Request.Form part of a Key Collection, thus 1 key example is:

"//IVS-NONINST-REC/IVS-NONINST-REC-REST[1]/IVS-SEC-DIAG-CDS[1]/IVS-SEC-DIAG-T[1]"

However, there are Attributes that I need to send over to have those parsed over and saved as well. I'm not sure how to send them over "properly" and if I send attribute over like

"//IVS-NONINST-REC/IVS-NONINST-REC-REST[1]/IVS-SEC-DIAG-CDS[1]/IVS-SEC-DIAG-T[1]/IVS-SEC-DIAG"

The code loop hits the throw new InvalidOperationException.

  1. How can I properly send over the attributes?
  2. How can I properly parse over the attributes in C# to save them?

The XML for that element and its attribute looks like this

<IVS-SEC-DIAG-T IVS-SEC-DIAG="C56.9" IVS-SEC-POA="">test</IVS-SEC-DIAG-T>

Update, complete xml (deleted some for brevity)

 <IVS-NONINST-REC xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <IVS-NONINST-REC-KEY IVS-REC-TYP="2" IVS-TM-STAMP="210549" IVS-ADJ-KEY="5">    
    </IVS-NONINST-REC-KEY>
    <IVS-NONINST-REC-REST IVS-PAT-ID="333089302">
      <IVS-SEC-DIAG-CDS>
       <IVS-SEC-DIAG-T IVS-SEC-DIAG="C56.9" IVS-SEC-POA="">test</IVS-SEC-DIAG-T>
    </IVS-NONINST-REC-REST>
</IVS-NONINST-REC>

While I can see all the xml elements and attributes in the xDoc InnerHTML, the xDoc thinks there are no attributes Here is a screencap

screencap of xDoc

  • I've read this a few times now, and I am not sure I fully understand. Let's start with this: 1) what exactly do you mean "send over"? 2) What is "manager"? 3) Have you verified that in xDoc there IS an item that matches your key? – Casey Crookston Jul 17 '20 at 20:14
  • Send Over - from html with ajax. For this attribute "IVS-SEC_DIAG" i'm just appending it to end after the element /IVS_SEC_DIAG-T[1] –  Jul 17 '20 at 20:19
  • Manager is XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable()); , I see that it does add a few namespaces like manager.AddNamespace("ns0", "..... –  Jul 17 '20 at 20:20
  • Yes - this IVS-SEC-DIAG" is a key that I added - but but sending XML data in request form to be parsed I'm not sure if the attributes should be added to the end ... –  Jul 17 '20 at 20:21
  • Have you tried `xDoc.SelectSingleNode(key)`? The namespace isn't required. – Casey Crookston Jul 17 '20 at 20:22
  • And also, can you show us the complete XML Node that you are trying to find? – Casey Crookston Jul 17 '20 at 20:23
  • Ok, @CaseyCrookston I added the snippet of xml to the bottom of the question , thx again –  Jul 17 '20 at 20:31
  • Have you studied this? Take a look, and if you still have trouble, come back and let me know. It looks like you are doing a few things wrong. https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=netcore-3.1 – Casey Crookston Jul 17 '20 at 20:42
  • I'm trying to study it, I added a screencap as well of the xDoc in VS studio watch –  Jul 17 '20 at 20:54
  • You should add `@` to attribute name in xpath. `"//.../IVS-SEC-DIAG-T[1]/@IVS-SEC-DIAG"` – Alexander Petrov Jul 17 '20 at 21:34
  • @AlexanderPetrov - YEP , that worked. A co-worker pointed that out. If you want to put up a quick answer, then I can properly credit you. thx! –  Jul 17 '20 at 22:12

1 Answers1

2

You should add @ to attribute name in xpath.

//.../IVS-SEC-DIAG-T[1]/@IVS-SEC-DIAG
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49