Development ASP.NET -> XML data handling...

Discussion in 'Software' started by LaughingJon, 7 May 2004.

  1. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    heya peeps

    I have an asp dot net file which is going to be called with an XML formatted page, however, as i dont have the 1st clue about dot NET is there any way i can easily handle this XML data. I need to pick out certain tags and depending on their value do *stuff* :hehe:

    I am sure there must be but google is letting me down :waah:

    pls help :D

    Cheers
    Jon

    *update.

    Ok i am even more lost after doing some reading lol :eyebrow:

    I have found code to read an xml page
    Code:
    reader = new XmlTextReader(filename);
    
    However, if my page is being called with the XML page how do i get that? Cos there is no physical file. Can u just use an Equiv of "GET" or something?
     
    Last edited: 7 May 2004
  2. Hepath

    Hepath Minimodder

    Joined:
    20 Oct 2003
    Posts:
    730
    Likes Received:
    0
    Sure
    you can load the xml into a document and traverse the nodes looking for those that conform to you requirements. But that is waay too slow.

    What you really need is to use the xpath objects. This is far the most efficent and effective way of extract information from an XML file when looking for specific data.

    Code:
    [size=2]
     
    using System.Xml.XPath;
    
    XPathDocument _xpathDoc = new XPathDocument(@"c:\file.xml");
    XPathNavigator _nav = _xpathDoc.CreateNavigator();
    
    string _xpathExpression = "..............";
    XPathNodeIterator iterator = _nav.Select(_xpathExpression);
    
    while (iterator.MoveNext())
    {
    	// do your stuff in here
    }
    [/size]
    
    if you dont know XPATH and need a hand post an example (small) of what your trying to extract and I'll give yo a hand.

    Stu
     
  3. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    Hi there,

    cheers for the reply.. so far i have been fiddling and not getting too far.. i have the following.

    Code:
    Imports System.Xml
    Imports System.Xml.Schema
    
    XmlNodeList smsc = node.SelectNodes("parent/level1")
    XmlElement element = smsc[ineedthisone]
    String strValue = element.InnerText
    
    however, i have just been informed that these commands come from a C# script so i am probably back at square 1 again lol.. I get errors at the imports alone.. lol..

    So basically i need to get the value of the item which is in "ineedthisone" i may need perhaps 1 or 2 more after but to get to the point where i have the value which is inside there would be fab!!

    cheers for any help :)

    Laters
    Jon

    *edit.. also note i dont have an actual file to refer too.. i only have the page which is calling it for 1 and for the other i have the page in 1 variable.. if that changes anything :)
     
  4. Hepath

    Hepath Minimodder

    Joined:
    20 Oct 2003
    Posts:
    730
    Likes Received:
    0
    OK,
    First off, ASP.NET is no longer akin to classic ASP; its now [effectilvely] a compiled language. This language being (normally) a choice of C# or VB.NET.

    My example will do exactly what you want, but is c# if you want it in VB.NET i'll translate it for you but it is a very simple operation. Just for the record there is no advantage in using either c# or VB.NET in ASP that we should be worried about. Tell me your language and I'll put all future examples in that. As my sig says, I'm a C# person, but VB is just as easy.

    The code you posted is what in fact I talked about in my first sentance. To be honest scrap it! Cut and past my code into a new C# WebApplication like so:
    1. Create the C# Web Application
    2. Add the "using" statements to the end of the list at the top of the file
    3. Add the code into the page_load function.

    You still dont say how you are being passed your XML but load it via file or string into the XPath Document.

    Specify an XPATH expression to get the values you want. This used via the navigator to return a list of nodes (zero or more) into the returned iterator. This iterator consists of a node list of the slected type that were specified by the XPath expression.

    XPATH Basics
    Xpath is not difficult it merely details how to 'walk' through a document in order to get the information you require. Its verbose though.

    Assume the following: XML document.
    <menu>
    <aperetif>
    <drink>Dry Sherry</drink>
    <drink type="red">Wine</drink>
    <nibbles count="20">Pringles</nibbles>
    <nibbles count="123">Twiglets</count>
    </aperetif>
    <starter>
    <sideplate count="23">Cold Toast</sideplate>
    <horsdeurve weight="15" unit="ounces">Pate Fois Gras</horsdeurve>
    </starter>
    <main>
    <vegetable>Potato</vegetable>
    <vegetable>Peas</vegetable>
    <vegetable>Carrot</vegetable>
    <meat>Lamb</meat>
    </main>
    <desert>
    <course>Gooseberry Fool<course>
    <option number="1">Cream</option>
    <option number="2">Custard</option>
    </desert>

    <afters>
    <drink>coffee</drink>
    <food>cheese &amp; biscuits</food>
    </afters>
    </menu>


    XPATH Expressions:
    1. Get a list of all the vegetables:
    a) //vegetable
    Gets all the vegetables in the WHOLE document
    b) /menu/main/vegatable
    Gets the vegetable elements under the menu/main tree

    2. Get me the nibbles where the count is greater than 50
    a) //nibbles[@count > 20]
    The @ sign denotes an attribute. The square brackets are a qualification.


    To get the level1 node from your example we would write:
    Parent/Level1
    Just as you did... or //Level1

    THere are a whole raft of XPATH expression and if you go to http://www.w3schools.com there is a great tutorial on it.
     
  5. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    Hi there,

    I think the main reasons for me having so many probs is that i am defo using VB for the rest of the page i am doing, which therefore isnt going to mix with this C# stuff is it?

    I see and understand the logic behind what you are saying though so many thanks for that, its just getting it working in my code which now remains lol.

    Also you mentioned this C# WebApplication i am using a text editor so are there certain things i need to put at the start of a Dotnet file for it to work properly?

    Cheers
    Jon
     
  6. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    omg i had to give up i couldnt get it working at all.. :wallbash: :waah: in the end i settled for instr("<tagname>") and a combination of mid n trim hahahha.. what a bodge!! but it is working for now.

    do you happen to know of any good resources for learning about .net be it c# or VB. i really want to get up to speed but i am very much a .Net NOOB!


    Cheers
    Jon
     
  7. Hepath

    Hepath Minimodder

    Joined:
    20 Oct 2003
    Posts:
    730
    Likes Received:
    0
    www.asp.net
    www.4guysfromrolla.com
    www.gotdotnet.com

    all the normal places ;)

    If you have a problem just google for the function or object you want to use with ".NET " and it wont take you long.

    Post a snippet of the XML file.... I'll crank {part of} the code using my stuff for ya.

    Stu
     
  8. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    heya,

    the xml form i have recieved is of the form..

    Code:
    <?xml version="1.0"?>
    <SMSDeliveryReport>
    <Operator>VF</Operator>
    <MessageTransactionID>24B3C1EB-65B7-4A09-8D0B-65FAD4E45C2C</MessageTransactionID>
    <MessageText>VF 23533420118033 SM DR</MessageText>
    <TransactionID>33016919-EDCF-4439-83CD-836A09A1D9B9</TransactionID>
    <DeliveryCode>0</DeliveryCode>
    <DeliveryMessage>Acknowledged (delivery confirmed)</DeliveryMessage>
    <Recipient>123456789123</Recipient>
    <Date>2002/08/03</Date>
    <Time>12:07:24</Time>
    </SMSDeliveryReport>
    
    from that the key things i need is the delivery code and the MessageTransactionID.

    so as you can tell my bodge is pretty much about as bodge like as you get hehehehe..

    cheers for the links as well
    Laters
    Jon
     
  9. Hepath

    Hepath Minimodder

    Joined:
    20 Oct 2003
    Posts:
    730
    Likes Received:
    0
    Funnily enough I'm doing some work using this now. Here's an example of using the iterator having read in a .NET csproj file (which are XML)
    Code:
    [SIZE=2]XPathNodeIterator iter = nav.Select("//References/Reference");
    while(iter.MoveNext())
    {
    item = lvwReferences.Items.Add(iter.Current.GetAttribute("Name", ""));
    item.SubItems.Add(iter.Current.GetAttribute("AssemblyName", ""));
    item.SubItems.Add(iter.Current.GetAttribute("HintPath", ""));
    }[/SIZE]
    
    Are you able to convert to VB.NET? Sorry a tadge busy at moment!

    Stu
     
  10. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    if you are busy then dont worry for now matey, just if you get a spare moment init :D the main thing is i have it working in one state or another atm, so this .net stuff is going to be done in my own time.

    i understand what you have written there and it does make sense, as for converting it to VB then hopefully if i read those websites you posted i will be able to do that :D

    cheers bud
    Jon
    :clap:
     
  11. Hepath

    Hepath Minimodder

    Joined:
    20 Oct 2003
    Posts:
    730
    Likes Received:
    0
    If I have time I'll convert to VB tonight far you.

    Stu
     

Share This Page