0

I have this problem to programming it. For each specific port element I want check if <script> exixst. The <script> is a child of <port> node element. Take this example, here's a link to the image:

https://i.stack.imgur.com/T2FGr.png

 // Leggi il file xml

        XDocument xml = XDocument.Load(pathFile.Text);



        var hosts = from h in xml.Descendants("address")
                   select new
                   {
                       addr = h.Attribute("addr").Value,
                       addrtype = h.Attribute("addrtype").Value
                   };

        foreach (var host in hosts)
        {
            if (host.addrtype == "ipv4")
            {
                console.Text += host.addr + Environment.NewLine;
                console.Text += host.addrtype + Environment.NewLine;

                var ports = xml.Descendants("port");

                foreach (var port in ports)
                {
                    var script = port.Element("script");

                    var hasScriptElement = script != null;

                    ?? -> how can i get the elem pem key value? thanks!!
                }


            }
        }
  • 1
    Cris please post some piece of your code. It quite difficult to help you without it – Christos Dec 20 '13 at 09:50
  • See this: http://stackoverflow.com/questions/12004903/check-xml-node-is-exist-or-not – Tony Dec 20 '13 at 09:56
  • Show us some example xml. Put all the text we need to know **here in the question**, not in an image off-site. – AakashM Dec 20 '13 at 10:09

4 Answers4

3

Using an XDocument...

var ports = document.Descendants("port");

foreach (var port in ports) {
    var script = port.Element("script");

    if (script != null)
    {
        var pem = script.Descendants("elem")
            .FirstOrDefault(n => n.Attribute("pem") != null);
    }
}
Fenton
  • 241,084
  • 71
  • 387
  • 401
0

Generic answer:

  • find <port and then >
  • find </port>
  • if between is a <script, then you have script
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • It is hard to tell what you are trying to ask or point out. Do you mean, what my answer doesn't answer on a question on a picture (how to get `` value)? Or do you mean what ` – Sinatr Dec 20 '13 at 09:58
0

Maybe you should consider using an XML Schema (XSD) and validate your XML against it. This should be considered as best practice!

You can found a guide how to validate your XML against an XSD programatically in C#.

Good luck!

Community
  • 1
  • 1
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
0

Here is a console app that demonstrates how this could be done:

class Program
{
    static void Main(string[] args)
    {
        string fileName = "c:\path\to\file\test.xml";
        var xmlDoc = new XmlDocument();
        xmlDoc.Load(File.OpenRead(fileName));
        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("port");
        XmlNodeReader nodeReader;
        foreach (var node in nodeList)
        {
            using(nodeReader = new XmlNodeReader((XmlNode)node))
            {
                if(nodeReader.ReadToDescendant("script"))
                {
                    Console.WriteLine("Found script tag");
                }
            }
        }
        Console.ReadKey();
    }
}
Aquila Sands
  • 1,471
  • 1
  • 20
  • 28