N
N
nikita__good2021-11-09 18:52:16
C++ / C#
nikita__good, 2021-11-09 18:52:16

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>

and a few more nodes with sub nodes
Need to write data to the structure
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;
            }
        }
    }

you need to write down the ip address, macaddress, port, port number, whether it is open or filtered.
C# code for parsing
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

1 answer(s)
I
Ilya Golets, 2021-11-18
@igolets

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

(briefly what happens here - a .cs file is created in the tmp directory so that the source is not overwritten in case of errors, then we rename the .cs to .designer.cs and place it in the main directory)
3. Both files (xsd and .designer.cs) can be added to the project, render the second nicely as nested in the first
4. Use the mentioned XmlSerializer to write
var serializer = new XmlSerializer ( typeof ( nmaprun ) );
                using ( var writer = new StreamWriter ( xmlFileName ) ) )
                {
                    serializer.Serialize ( writer, item );
                    writer.Close ();
                }

and for reading
using ( var temp = new StringReader ( xmlText ) )
                    {
                        var tmp = serializer.Deserialize ( temp );
                        return ( nmaprun ) tmp;
                    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question