Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need lots of help parsing XML 2

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hello all,
I am new to C Sharp and I am in need of help to parse a nessus XML file. I am un able to recursively go through an XML file and look for the elements that I want. Does anyone have any sample code that recursively gets child elements?

Again I am new so the code that I have been using just get top level elements.

Thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
google is your friend. you will also want to research the System.Xml namespace.

I am unable to recursively go through an XML file and look for the elements that I want.
this doesn't make any sense. xml is just text. you have to be able to load the xml to parse it. therefore you can recruse through file.
now if this is a restriction of the requirements you were given. That makes me think:
1. this is a homework assignment
2. this academic in nature (but not homework)
3. your business requirements are very strict
4. you cannot because you don't know how.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
No, I am used to doing this in perl, but for this project they wanted c# (windows environment). I am a systems engineer by trade, my boss wanted me to pares a bunch of nessus (vulnerability scanner) files and add them to a csv file. Here is my problem, I have never played with XML before and have tried may things on the MSDN site. I want to loop and get children of certain tags. Here is a brief example
Code:
<ReportHost>
   <HostName>blabla</HostName>
    <ReportItem>
     <port>netbios-ssn (139/tcp)</port> 
     <severity>0</severity> 
     <pluginID>0</pluginID> 
     <pluginName /> 
     <data>PORT</data> 
   </ReportItem>
   <ReportItem>
    <port>microsoft-ds (445/tcp)</port> 
    <severity>0</severity> 
    <pluginID>0</pluginID> 
    <pluginName /> 
    <data>PORT</data> 
  </ReportItem>
</ReportHost>

I am not sure how many <ReportItem></ReportItem> I am going to have so I thought that I would have to loop through the file and get them all.

To answer your question, I have been playing with c# for about 3 weeks and it is new to me. I guess I am #4 on your selection. I come from a Unix/Linux world, trying to help the Windows guys/gals out. I will again search the web.

Thanks for the response.

timgerr


-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
there are 3 concepts in your project you are merging.
1. xml and xpath queries (traversing xml)
2. the .net framework
3. the c# language

I find is a good quick and dirty kickstart site. this should help with the xpath and xml stuff.

as for c#...
if you are using .net 3.x you could google LinqToXml. which is like an agnostic query language that can parse xml. If you are not using 3.x. Look into the XmlDocument object. this allows you to traverse and minipulate xml easily. If preformance becomes a problem you can also look into the xmoreader object. But i wouldn't worry about that until it's a problem.

here is a snippet of what your code may look like
Code:
var document = new XmlDocument();
document.Load("path to xml file");
var nodes = document.SelectChildNodes("\\ReportItem");

foreach(var node in nodes)
{
   var port = node.SelectNode("port").Text;
   var severity = int.Parse(node.SelectNode("servity").Text);

   // write values to csv file
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the help, I will give that a try.
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
is this an example of LinqToXml????

thanks again,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
the code snippet i provided is not linqtoxml. that is plain old 2.0 compatible .net objects. Linq takes 2 forms.
1. is the typical OOP syntax
get_collection_of_something().Where(..).OrderBy(...);

2. is the linq syntax sugar
from x in get_collection_of_something()
where ...
order by ...
select ...

if you think this looks alot like tsql you're correct. the concept of link is to query collections of things (objects, databases, xml, web services)

I prefer the OOP syntax over the tsql style, but that's just preference.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
<is this an example of LinqToXml????

Hi Tim,

I would suggest that you research the System.XML namespace a bit, as Jason mentions. You'll find it here (easy to find using google): You'll see that the XMLDocument class is a member of this namespace.

While it's quite possible to parse XML simply by manipulating text files, it's kind of reinventing the wheel. The System.XML namespace contains a wide range of classes that are designed to easily do this, as well as perform many other operations on XML files.

Bob
 
OK, I am now getting this, I have a new question for ya'll if that is ok. I am writing a recursive routine to get what I am looking for. I am having troubles with a method, I am not sure that to declare the argument. Here is my code
Code:
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"c:\testscript\nessus\nessusout.xml");
            XmlNodeList nodeList = doc.GetElementsByTagName("ReportHost");
            foreach (XmlNode node in nodeList)
            {
                Recurse(node);
            }
                
        }

        public void Recurse(XmlNode meNode)
        {

        }
}

so I am going to get the nodeList and look through it with a foreach. I want to pass the node information into another method but I am getting an error here
Code:
 public void Recurse(XmlNode meNode)
{

}
I am getting an error
Code:
 Error	1	An object reference is required for the non-static field, method, or property 'XMLv1.Program.Recurse(System.Xml.XmlNode)'	C:\Documents and Settings\My Documents\Visual Studio 2008\Projects\XMLv1\XMLv1\Program.cs	18	17	XMLv1
for some reason I am unable to declare XmlNode meNode, I think that I am handing an object to Recurse() and it is looking for a single node?????

Not sure what the problem is, thanks for all the help.
timgerr


-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
recurse needs to be static because your are calling it from the static member Main.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
To elaborate on Jason's answer just a bit: static means that the member belongs to the type rather than the instance. To get a better understanding of what that means, read
So, there's only one copy of static stuff, that all instances access in common. Stuff that isn't static has one copy per instance. That's why you can't call a non-static method from a static one, because it wouldn't know which instance's method to call.

Since you always enter the class in the same way, instances of a class share a common entry point. That's why main is static.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top