Help with passing values to dataelement based on partial string

vcharles
vcharles used Ask the Experts™
on
Hi,

How d I modify the line below to look for part of AGD4, for example AGD4 contains data in the following format (XXXX-XX-XXXX) and I need to look for the mIddle value (-XX-) instead of the entire value of AGD4.


Group n By n.Element("AGD4").Value.substring (??)  Into agd4Group = Group

  '' Load the XML document to be summerized into memory
        Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP5.xml")

        '' Group the TableA nodes by AGD4 then get a string containing all the Country values order the Country values
        '' and return the groupings and summary string so that it can be inserted into the XML                                                                                                                      
        Dim results = (From n In xdoc.Descendants("AOP5C") Where n.Element("AGD4").Value <> "NA"
                       Group n By n.Element("AGD4").Value Into agd4Group = Group
                       Let sum = agd4Group.Select(Function(g) g.Element("COUNTRY").Value).OrderBy(Function(g) g).ToArray() _
                       From g In agd4Group _
                       Let summary = String.Join(",", sum) _
                       Select g, summary).ToList()

        '' Update the Summary node with the value of the summary string.                                                                                                                      
        results.ForEach(Sub(m) m.g.Element("CANMN").Value = m.summary)
        '' Save the document.                                                                                                                      
        xdoc.Save(Application.StartupPath + "\AOP5.xml")

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi,

How do I include Substring(AGD4, 5, 3) in Group n By n.Element("AGD4").Value.substring (??)  Into agd4Group = Group?

Victor
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Victor;

Have you tried it this way?

Group n By n.Element("AGD4").Value.SubString(5, 2)  Into agd4Group = Group

Which will make the grouping the two middle characters, YY, in the pattern XXXX-YY-XXXX.

Author

Commented:
Hi,

I tied it but received error message

Range variable name can  be inferred only from a simple or qualified name with no arguments.

Thanks,

Victor
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Retired
Distinguished Expert 2017
Commented:
Hi Victor;

Because the grouping value is not a simple variable but an expression we need to give it a name.

Group n By Key = n.Element("AGD4").Value.SubString(5, 2)  Into agd4Group = Group

Author

Commented:
Thank You.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
No problem Victor, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial