[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

Help Parsing XML in C Sharp

Experts,
 I am TRYING to do something, supposibly simple in C#, which i know how to do in ASP.NET
Please Help

I have the code for both form data and Code behind

I simple want to pass the Path of the XML to the Function and have it display data

HELP PLEASE


//--------------------Form
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ssrf._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%
                 string xmlPath="c:\itt\7763V.xml";  
                ProcessXml(xmlPath); 
     %>
    </div>
    </form>
</body>
</html>
//-------------------Code Behind--------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ssrf
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             
        }


        private void ProcessXml(string xmlPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(xmlPath);

            // Set up namespace manager for XPath    
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);



            nsMgr.AddNamespace("ddms", "urn:us:gov:dod:standard:ssrf:1.2.4.b");
            nsMgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            nsMgr.AddNamespace("s", "urn:us:gov:dod:standard:ssrf:1.2.4.b");
            nsMgr.AddNamespace("my", "http://metadata.dod.mil/mdr/ns/SSRF/1.2.4.b/ssrf.xsd");


            XmlNodeList txrx2 = doc.SelectNodes("s:SSRF/s:Body/s:TxRx", nsMgr);

            foreach (XmlNode node in txrx2)
            {
                WalkNodes(node);
            }
        }

        private void WalkNodes(XmlNode iXmlNode)
        {
            if (iXmlNode.InnerText != null)
            {
                Response.Write("Element - " + iXmlNode.Name + ": " + iXmlNode.InnerText + "<br>");
            }
            Response.Write(iXmlNode.InnerXml);
            if (iXmlNode.Attributes != null)
            {
                if (iXmlNode.Attributes.Count > 0)
                {
                    Response.Write(iXmlNode.Name + " Attributes: " + "<br>");
                    int Atcount;
                    //Atcount = (iXmlNode.Attributes.Count - 1);
                    //for (int atrib = 0;Atcount;atrib++) //<---- error i get is - Cannot convert type 'int' to 'bool'
                    //{
                    //    Response.Write("     " + iXmlNode.Attributes(atrib).Name + " - " + iXmlNode.Attributes(atrib).Value + "<br>");
                    //error i get is - System.XML.XMLNode.Attributes is a 'property' but used like a 'method'
                    // } 


                    // Get the node's attributes
                    XmlAttributeCollection attCol = iXmlNode.Attributes;

                    // Print them for testing
                    Response.Write(attCol[0].Value + "<br/>");

                }
            }

            foreach (XmlNode child in iXmlNode.ChildNodes)
            {
                WalkNodes(child);
            }


        } 
           

    }
}

Open in new window

ssrf2.zip
0
dsiemon
Asked:
dsiemon
1 Solution
 
Bob LearnedCommented:
What version of ASP.NET do you have?  You might be able to use an XmlDataSource...
0
 
AshokCommented:
Atcount = (iXmlNode.Attributes.Count - 1);
                   
for (int atrib = 0;atrib <= Atcount;atrib++) //<---- error i get is - Cannot convert type 'int' to 'bool'
{
  Response.Write("     " + iXmlNode.Attributes(atrib).Name + " - " + iXmlNode.Attributes(atrib).Value + "<br>");
}

HTH
Ashok
0
 
anarki_jimbelCommented:
OK, I believe  you need to use square brackets to supply index for attributes:

 iXmlNode.Attributes[atrib].Name

ashok111 has fixed another error, I hope
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now