user2738563
user2738563

Reputation: 13

how to store XML tags and retrieve data

I am having a hard time to understand this code, can anyone help me with this? If I'm not wrong, StrStaffInfo is used to store the XML tags.

I wanted to know how the tags are stored in the XML file, and how to access those Tags?

<script runat="server">
Public StaffInfoXML as new xmlDocument

sub Page_Load(obj as object, e as eventargs)

    Dim StrStaffInfo, strConnXMLData, strConnStr, SpString as String
    Dim oStaffDAW as New DAWNET.DataAccess.XMLWrapper.DAW()
    Dim oConn as new ConnectionString.Access.DataBase.IO.ConnectionString

    ' Fetch the Connection String from Configuration File
    oConn.SetPath(MapPath(""))
    strConnStr = oConn.FetchConnectionString(strConnXMLData)


    SpString = "StaffInformationSelect '"& Decode(LOGONUSER.ToString) &"','"& Decode(CLIENTIP.ToString) &"','"& Decode(MACHINENAME.ToString) &"'"
    oStaffDAW.FetchXml(strConnStr, SpString, StrStaffInfo)

            StaffInfoXML.LoadXml(StrStaffInfo)



    if (StaffInfoXML.GetElementsByTagName("RECORD").Count = 0)
        Response.Redirect("Application.aspx")
        Response.End
    End if

end sub

Where is the TagName "record" stored? I want to include another Tag, and check a If condition.

Upvotes: 0

Views: 311

Answers (2)

Concrete Gannet
Concrete Gannet

Reputation: 580

Be careful with terminology. A tag is just text delimited by less-than and greater-than characters. An element has opening and closing tags, and may have additional content between the two. Elements are the important thing.

The code is calling StaffInformationSelect, which appears to be a stored procedure in your database that produces an XML result. Yes, StrStaffInfo should hold the result as a string containing XML.

The XML result is then loaded into the StaffInfoXML object, which is an instance of XMLDocument, the .NET XML DOM (Document Object Model) class.

Then it's looking to see if the XML document has any RECORD elements, and if there are none, redirecting to another page. If the document looked like this:

<?xml version="1.0"?> <root> <RECORD></RECORD> <RECORD></RECORD> </root>

the Count property would have a value of 2.

Apart from the GetElementsByTagName method you see in the existing code, check out GetElementById, which will locate an element by its unique ID.

The tag name is defined by StaffInformationSelect. It might correspond to a table name in your database.

GetElementsByTagName will produce a collection of XmlNode objects, which you could iterate through. If you want to do complex searching for some elements in the document, check out XPathDocument, or perhaps the new XDocument, which allows you to run LINQ queries on an XML document.

If you can attach a debugger to this code, set a breakpoint on the line that calls LoadXML, and you should see the XML document in the StrStaffInfo variable.

Upvotes: 1

Tim
Tim

Reputation: 28530

Note a direct answer to your question, but strStaffInfo is a string with the XML, and it is being loaded into an XmlDocument (StaffInfoXML) with the following line:

StaffInfoXML.LoadXml(StrStaffInfo)

The tag RECORD is somewhere in the document tree. GetElementsByTagName returns an XmlNodeList of all the tags (nodes) and their children that have the name "RECORD" in the document.

If you know the name of the element (tag) you're looking for, you can make another call to GetElementsByName, and then check the value(s) of that/those element(s) for your If condition.

I would also take a look at LINQ to XML (which is my preferred method for parsing XML).

Upvotes: 1

Related Questions