Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Visual Basic 2008 Express Edition!
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
"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.
Posted

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

Visual Basic 2008 Express Edition!
Posted

"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.

Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...