Answer the question
In order to leave comments, you need to log in
How to parse an XML document in C#?
I have an Xml document.
<nmaprun>
<host starttime="1636452524" endtime="1636452524">
<status state="up" reason="arp-response" reason_ttl="0" />
<address addr="192.168.5.1" addrtype="ipv4" />
<address addr="02:20:80:A8:F9:4B" addrtype="mac" />
<hostnames />
<ports>
<extraports state="closed" count="994">
<extrareasons reason="reset" count="994" proto="tcp"/>
</extraports>
<port protocol="tcp" portid="21">
<state state="open" reason="syn-ack" reason_ttl="64" />
<service name="ftp" method="table" conf="3" />
</port>
<port protocol="tcp" portid="22">
<state state="filtered" reason="port-unreach" reason_ttl="64" />
<service name="ssh" method="table" conf="3" />
</port>
<port protocol="tcp" portid="23">
<state state="filtered" reason="port-unreach" reason_ttl="64" />
<service name="telnet" method="table" conf="3" />
</port>
<port protocol="tcp" portid="53">
<state state="open" reason="syn-ack" reason_ttl="64" />
<service name="domain" method="table" conf="3" />
</port>
<port protocol="tcp" portid="80">
<state state="open" reason="syn-ack" reason_ttl="64" />
<service name="http" method="table" conf="3" />
</port>
<port protocol="tcp" portid="2103">
<state state="filtered" reason="port-unreach" reason_ttl="64" />
<service name="zephyr-clt" method="table" conf="3" />
</port>
</ports>
<times srtt="1277" rttvar="990" to="100000" />
</host>
</nmaprun>
public class ParsBase
{
List<HostInfo> hostInfo;
struct HostInfo
{
public string ip, mac;
List<PortInfo> ports;
struct PortInfo
{
public string nameport;
public int num;
public bool isOpen;
public bool isFiltered;
}
}
}
static class Parsing //парсинг результатов работы nmap
{
public static void ParsingReport(string path)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
// получим корневой элемент
XmlElement xMain1 = xDoc.DocumentElement;
// обход всех узлов в корневом элементе
foreach (XmlNode xnode2 in xMain1)
{
}
}
}
Answer the question
In order to leave comments, you need to log in
How do I usually deal with this?
1. Make (or take from an XML data provider) a file with an XSD schema
2. Make a C# file from the XSD with the commands:
xsd SourceFormat.xsd /c /n:NameSpace /o:tmp
cd tmp
ren SourceFormat.cs SourceFormat.designer.cs
move /y SourceFormat.designer.cs ..
cd ..
rd /s/q tmp
var serializer = new XmlSerializer ( typeof ( nmaprun ) );
using ( var writer = new StreamWriter ( xmlFileName ) ) )
{
serializer.Serialize ( writer, item );
writer.Close ();
}
using ( var temp = new StringReader ( xmlText ) )
{
var tmp = serializer.Deserialize ( temp );
return ( nmaprun ) tmp;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question