vbMarkO Posted July 27, 2006 Posted July 27, 2006 I have created a xml file of which I am adding records to and thus they are not being added by me the user in a sorted order.... So, my thought is, could I call for a sort routine that would sort it? If so could you point me to it so I might see how this is done... My xml is somewhat like this <ChurchMemberShip> <Members> <Name>Smith, John</Name> <Address>Here Street</Address> </Members> <Members> <Name>Davis, Bob</Name> <Address>There Street</Address> </Members> </ChurchMemberShip> Could I sort this to look like this <ChurchMemberShip> <Members> <Name>Davis, Bob</Name> <Address>There Street</Address> </Members> <Members> <Name>Smith, John</Name> <Address>Here Street</Address> </Members> </ChurchMemberShip> If so how? vbMarkO Quote Visual Basic 2008 Express Edition!
Cags Posted July 27, 2006 Posted July 27, 2006 To the best of my knowledge there is no way of sorting an xml file. If you wish to sort the data contained in the xml file, the only way I know of achieving it is to load the data into some kind of array sort it in your application. Then write out the array as a new xml file. Quote Anybody looking for a graduate programmer (Midlands, England)?
mskeel Posted July 27, 2006 Posted July 27, 2006 "Sorting" goes against the whole concept of XML really. The order of the nodes in the file don't matter at all -- it's all about the tags and hierarchy of the nodes in relation to one another. Cags is right, you'll have to put the data into some kind of an array or list and sort it yourself when you need it. Quote
vbMarkO Posted July 27, 2006 Author Posted July 27, 2006 Ok, Makes sense! I know I can sort it in a control as well such as a Combo or listbox. Though it would seem, I could also use the Insertafter or insertbefore to make it search for the right place to place the node perhaps so as it writes to the xml it is putting it in a sorted order..... Does this sound reasonable? Or should I just forgo that and simply add the contents of the xmlfile to an array and sort the array then simply overwrite the existing xml with the sorted array? vbMarkO Quote Visual Basic 2008 Express Edition!
mskeel Posted July 28, 2006 Posted July 28, 2006 "Order" is an arbitrary concept in XML. I wouldn't worry about it to much. That tags are what hold the information. If you really, really needed to have your XML sorted (even though it makes no sense) then writing the nodes out from a sorted list would do the trick. And again, reading them in and sorting them yourself when you need them sorted would also work for whatever application you are making... I've never used InsertBefore or InsertAfter -- so knowing nothing about them I'd wager that getting that technique to work for you will be more work than it is worth. You'd probably have to navigate the DOM to figure out where you want to make your insert which will be, essentially, a linear search for every insert. You'd be better off using a quick sort algorithm in a List and writing it out in order. But again...it doesn't really matter because XML order doesn't make sense. Context is taken from the tags, not the order of nodes. Quote
Administrators PlausiblyDamp Posted July 28, 2006 Administrators Posted July 28, 2006 Or you could always use XSLT to transform the source into a sorted document. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted July 28, 2006 Posted July 28, 2006 XSLT doesn't change node ordering in the document, though. (Not that it matters because that sounds like a great solution) vbMarkO, why do you need your XML document sorted? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.