1

I am using the following code to parse a XML file of mine:

Dim xml As String = "<?xml version=""1.0"" encoding=""Windows-1252""?>" & _
                        "<theref:theref-msg xmlns:csr=""http://www.xxxxx.com/Schema/csr"" xmlns:theref=""http://www.xxxxx.com/Schema/theref"">" & _
                          "<theref:header>" & _
                            "<theref:eid />" & _
                            "<theref:reference_id>429</theref:reference_id>" & _
                            "<theref:sr_type_code>US1</theref:sr_type_code>" & _
                            "<theref:event_type_code>REQUEST</theref:event_type_code>" & _
                            "<theref:eai_event_code>DSR</theref:eai_event_code>" & _
                            "<theref:source_code>WORKS</theref:source_code>" & _
                            "<theref:target_code>APP</theref:target_code>" & _
                            "<theref:status_code />" & _
                            "<theref:details />" & _
                          "</theref:header>" & _
                        "</theref:theref-msg>"

    Dim document As XDocument = XDocument.Parse(xml)

    Dim pupils = From pupil In document.Descendants("theref:theref-msg") _
                 Select New With _
                 { _
                    .Name = pupil.Element("theref:reference_id").Value, _
                    .TagID = pupil.Element("theref:sr_type_code").Value _
                 }

    For Each pupil In pupils
        Debug.Print("{0}: {1}", pupil.Name, pupil.TagID)
    Next

The problem being is that it doesn't seem to work at all. It crashes on the line:

Dim pupils = From pupil In document.Descendants("csreai:csreai-msg") _
                 Select New With _
                 { _
                    .Name = pupil.Element("csreai:reference_id").Value, _
                    .TagID = pupil.Element("csreai:sr_type_code").Value _
                 }

ERROR is: A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll The ':' character, hexadecimal value 0x3A, cannot be included in a name. 5

UPDATED CODE:

Dim xml As String = "<?xml version=""1.0"" encoding=""Windows-1252""?>" & _
                    "<theref:theref-msg xmlns:csr=""http://www.xxxxx.com/Schema/csr"" xmlns:theref=""http://www.xxxxx.com/Schema/theref"">" & _
                      "<theref:header>" & _
                        "<theref:eid />" & _
                        "<theref:reference_id>429</theref:reference_id>" & _
                        "<theref:sr_type_code>US1</theref:sr_type_code>" & _
                        "<theref:event_type_code>REQUEST</theref:event_type_code>" & _
                        "<theref:eai_event_code>DSR</theref:eai_event_code>" & _
                        "<theref:source_code>WORKS</theref:source_code>" & _
                        "<theref:target_code>APP</theref:target_code>" & _
                        "<theref:status_code />" & _
                        "<theref:details />" & _
                      "</theref:header>" & _
                      "<theref:body>" & _
                        "<csr:document>" & _
                          "<csr:header>" & _
                            "<csr:system>CSR</csr:system>" & _
                            "<csr:doc_name>FULLSR</csr:doc_name>" & _
                            "<csr:version>3.1</csr:version>" & _
                            "<csr:dml_event>UPDATE</csr:dml_event>" & _
                          "</csr:header>" & _
                    "</csr:document></theref:body></theref:theref-msg>"

Dim xmlb = From getXMLData In document.<theref:theref-msg>.<theref:header>.<theref:body>.<csr:document>.<csr:header>

Newest UPDATE

What if i have this:

   <csr:custom_attributes>
      <csr:custom_attribute>
        <csr:type_code>
          <csr:value>data1</csr:value>
        </csr:type_code>
        <csr:group_code>
          <csr:value>wide1</csr:value>
        </csr:group_code>
      </csr:custom_attribute>
      <csr:custom_attribute>
        <csr:type_code>
          <csr:value>data2</csr:value>
        </csr:type_code>
        <csr:group_code>
          <csr:value>wide2</csr:value>
        </csr:group_code>
      </csr:custom_attribute>
   </csr:custom_attributes>

I can only seem to get the first set of data (data1, wide1) but not the second?

   xmlDATA = (From getXMLData In document.<theref:csreai-msg>.<theref:body>.<csr:document>.<csr:service_request>.<csr:custom_attributes>.<csr:custom_attribute>).ToList()
StealthRT
  • 10,108
  • 40
  • 183
  • 342

2 Answers2

1

1.Try with this,

       document.Descendants("{http://www.xxxxx.com/Schema/theref}theref-msg").FirstOrDefault

2.Another solution with traditional looping approach,

     Imports <xmlns:ns='http://www.xxxxx.com/Schema/theref'> 

import namespace on top of the class. than use follwoing code to get the values,

         For Each header As XElement In document.<ns:theref-msg>.<ns:header>.<ns:reference_id>
             dim something = header.Value
         Next
indiPy
  • 7,844
  • 3
  • 28
  • 39
1

At the top of your code above any class/namespace declarations import the theref namespace:

Imports <xmlns:theref="http://www.xxxxx.com/Schema/theref">

Then you can just use XML literals to select with

Option Explicit On
Option Strict On

Imports <xmlns:theref="http://www.xxxxx.com/Schema/theref">

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim xml As String = "<?xml version=""1.0"" encoding=""Windows-1252""?>" & _
                        "<theref:theref-msg xmlns:csr=""http://www.xxxxx.com/Schema/csr"" xmlns:theref=""http://www.xxxxx.com/Schema/theref"">" & _
                          "<theref:header>" & _
                            "<theref:eid />" & _
                            "<theref:reference_id>429</theref:reference_id>" & _
                            "<theref:sr_type_code>US1</theref:sr_type_code>" & _
                            "<theref:event_type_code>REQUEST</theref:event_type_code>" & _
                            "<theref:eai_event_code>DSR</theref:eai_event_code>" & _
                            "<theref:source_code>WORKS</theref:source_code>" & _
                            "<theref:target_code>APP</theref:target_code>" & _
                            "<theref:status_code />" & _
                            "<theref:details />" & _
                          "</theref:header>" & _
                        "</theref:theref-msg>"

        Dim document As XDocument = XDocument.Parse(xml)

        Dim pupils = From pupil In document.<theref:theref-msg>.<theref:header>
                     Select New With
                            {
                                .Name = pupil.<theref:reference_id>.Value,
                                .TagID = pupil.<theref:sr_type_code>.Value
                            }


        Dim pupilList = pupils.ToList()

        For Each pupil In pupilList
            Debug.Print("{0}: {1}", pupil.Name, pupil.TagID)
        Next
    End Sub
End Class

EDIT

You need to Import every namespace that you want to use in an XML literal. So your import should now be:

Imports <xmlns:theref="http://www.xxxxx.com/Schema/theref">
Imports <xmlns:csr="http://www.xxxxx.com/Schema/csr">

That will clean up the compilation errors. Next you also need to remove <theref:header> from the query since <theref:body> is a sibling to it and not a child. When debugging these kind of things I also recommend always using ToList(). When you got to production you can remove that for performance reasons (I usually don't bother).

Dim xmlb = (From getXMLData In document.<theref:theref-msg>.<theref:body>.<csr:document>.<csr:header>).ToList()

EDIT 2

Here's the full code using your new XML data. When I run it I get a messagebox that says "CSR"

Option Explicit On
Option Strict On

Imports <xmlns:theref="http://www.xxxxx.com/Schema/theref">
Imports <xmlns:csr="http://www.xxxxx.com/Schema/csr">

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim xml As String = "<?xml version=""1.0"" encoding=""Windows-1252""?>" & _
                     "<theref:theref-msg xmlns:csr=""http://www.xxxxx.com/Schema/csr"" xmlns:theref=""http://www.xxxxx.com/Schema/theref"">" & _
                       "<theref:header>" & _
                         "<theref:eid />" & _
                         "<theref:reference_id>429</theref:reference_id>" & _
                         "<theref:sr_type_code>US1</theref:sr_type_code>" & _
                         "<theref:event_type_code>REQUEST</theref:event_type_code>" & _
                         "<theref:eai_event_code>DSR</theref:eai_event_code>" & _
                         "<theref:source_code>WORKS</theref:source_code>" & _
                         "<theref:target_code>APP</theref:target_code>" & _
                         "<theref:status_code />" & _
                         "<theref:details />" & _
                       "</theref:header>" & _
                       "<theref:body>" & _
                         "<csr:document>" & _
                           "<csr:header>" & _
                             "<csr:system>CSR</csr:system>" & _
                             "<csr:doc_name>FULLSR</csr:doc_name>" & _
                             "<csr:version>3.1</csr:version>" & _
                             "<csr:dml_event>UPDATE</csr:dml_event>" & _
                           "</csr:header>" & _
                     "</csr:document></theref:body></theref:theref-msg>"

        Dim document As XDocument = XDocument.Parse(xml)

        Dim xmlb = (From getXMLData In document.<theref:theref-msg>.<theref:body>.<csr:document>.<csr:header>).ToList()
        MsgBox(xmlb.<csr:system>.Value)

    End Sub
End Class

EDIT 3

Well, you only gave me part of your XML ;) so I had to make some of it up. The trick here is that getXMLData will be an XElement, specifically a <csr:custom_attribute> node. With that you need to go back to your original code and use the Select New... code.

For convenience I converted your XML string to a raw XDocument because all of the concats was making it hard to read. Remember, my XML might not exactly match yours because you only gave me a portion.

Option Explicit On
Option Strict On

Imports <xmlns:theref="http://www.xxxxx.com/Schema/theref">
Imports <xmlns:csr="http://www.xxxxx.com/Schema/csr">

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim document = <?xml version="1.0" encoding="Windows-1252"?>
                       <theref:theref-msg xmlns:csr="http://www.xxxxx.com/Schema/csr" xmlns:theref="http://www.xxxxx.com/Schema/theref">
                           <theref:body>
                               <csr:document>
                                   <csr:service_request>
                                       <csr:custom_attributes>
                                           <csr:custom_attribute>
                                               <csr:type_code>
                                                   <csr:value>data1</csr:value>
                                               </csr:type_code>
                                               <csr:group_code>
                                                   <csr:value>wide1</csr:value>
                                               </csr:group_code>
                                           </csr:custom_attribute>
                                           <csr:custom_attribute>
                                               <csr:type_code>
                                                   <csr:value>data2</csr:value>
                                               </csr:type_code>
                                               <csr:group_code>
                                                   <csr:value>wide2</csr:value>
                                               </csr:group_code>
                                           </csr:custom_attribute>
                                       </csr:custom_attributes>
                                   </csr:service_request>
                               </csr:document>
                           </theref:body>
                       </theref:theref-msg>

        Dim xmlDATA = (
                        From getXMLData In document.<theref:theref-msg>.<theref:body>.<csr:document>.<csr:service_request>.<csr:custom_attributes>.<csr:custom_attribute>
                        Select New With {.TypeCode = getXMLData.<csr:type_code>.<csr:value>.Value, .GroupCode = getXMLData.<csr:group_code>.<csr:value>.Value}
                      ).ToList()
        For Each X In xmlDATA
            Trace.WriteLine(X.TypeCode & ":" & X.GroupCode)
        Next
    End Sub
End Class
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Doesnt seem to find anything. It skips the **Debug.Print("{0}: {1}", pupil.Name, pupil.TagID)** code. – StealthRT Apr 12 '11 at 21:34
  • You're using VB2010, right? If you create a brand new Windows Forms Application and then copy the entire code above over the contents of `Form1.vb` it should kick out `429: US1` to the Immediate Window. Does this not happen? – Chris Haas Apr 12 '11 at 21:52
  • Yes i'm using VB2010. But the code above does not still seem to work. **pupilList** keeps saying its **0**. – StealthRT Apr 13 '11 at 12:24
  • @StealthRT, I don't know what to tell you. I've tried this multiple times on multiple machines. If I copy the code _exactly_ as above and replace everything in `Form1.vb` of a brand new default Windows Forms Application it always outputs `429: US1` to the Immediate Window. Did you try integrating the above into your code or have you tried creating a blank project with only the above? – Chris Haas Apr 13 '11 at 13:52
  • @Chris: Yes, it did work once i put it into its own form... but why is that? It's practically the same as my first form? – StealthRT Apr 14 '11 at 13:47
  • @StealthRT, I wanted to test whether my code worked for you exactly without any other code to possibly interfere. The difference between "practically the same" and "the same" could be one character that you think shouldn't matter but could break everything. Once you and I can agree together on something that both works for us we can find out the differences with what doesn't work. But if this code, copied exactly into a brand new Windows Form Application with no other code of yours doesn't work for you then unfortunately I'm not able to help you further, and I'm sorry for that. – Chris Haas Apr 14 '11 at 14:08
  • @Chris: Hey i understand! Thanks for all the help so far! I have updated my OP with some more of my XML to gather. I seem to be getting empty data for the second set of values and i'm not sure if thats due to the schema name changing from **theref** to **csr**? – StealthRT Apr 14 '11 at 14:26
  • @Chris: Thanks for updating your code. I put it in and i seem to still get blank data out. **Dim xmlb = (From getXMLData In document....).ToList()** and i check it by doing this **MsgBox(xmlb..Value)** but it is blank. – StealthRT Apr 14 '11 at 14:52
  • @Chris: Any other suggestions? – StealthRT Apr 15 '11 at 12:04
  • @StealthRT, did you try running the code in the **EDIT2** section above? – Chris Haas Apr 15 '11 at 12:59
  • @Chris: I just ran it. I still get a value of **0** for the xmlb. – StealthRT Apr 15 '11 at 13:50
  • 1
    @StealthRT, unfortunately we're at an impasse again. I just tested the above code (once again, _exactly_) and it popped up `CSR` for me. – Chris Haas Apr 15 '11 at 15:42
  • @Chris: Can you zip that up and post it for me to download and see if it works on mine? – StealthRT Apr 15 '11 at 15:52
  • 1
    Here's my project http://chris.vendiadvertising.com/Stackoverflow/XML-5640488/WindowsApplication10.zip – Chris Haas Apr 15 '11 at 15:58
  • @Chris: Ah! I see the problem... I copied the imports with the xxxxx.com instead of what it should be pointing too! And yes, now it works just fine :o) THANKS CHRIS!! – StealthRT Apr 15 '11 at 16:04
  • @Chris: I ran into another snag. Check my newest update in my OP, please :o) – StealthRT Apr 15 '11 at 16:23
  • @StealthRT, I added another *EDIT 3* above. If you have any questions about this ask here but if you have another tangentially related question it would probably be best to open another question and just reference this one. People reading this in the future might have a hard time following the entire trail here. – Chris Haas Apr 15 '11 at 16:52
  • @Chris: Great, i got it working! :o) YAY! Thanks a lot, CHRIS! – StealthRT Apr 15 '11 at 18:28