none
xml elements and attributes to array or string[] RRS feed

  • Question

  • Hi,

    I'm trying to read all xml elements and attributes in a class to an array or string[], and assign to variables and use them in my main program. 

    XML file 

    <?xml version="1.0" encoding="utf-8"?>
    <CONFIG>
      <SERVIDOR ID="1" nome="ISS001" ip="10.0.0.1" versao="0.0.0">
        <POS ID="1" nome="pos001" ip="127.0.0.1" versao="0.0.0" />
    	<POS ID="2" nome="pos002" ip="127.0.0.1" versao="0.0.0" />
      </SERVIDOR>
      <SERVIDOR ID="2" nome="ISS002" ip="10.0.0.1" versao="0.0.0">
        <POS ID="1" nome="pos001" ip="127.0.0.1" versao="0.0.0" />
    	<POS ID="2" nome="pos002" ip="127.0.0.1" versao="0.0.0" />
      </SERVIDOR>
    </CONFIG>

    Method from class "configXML"

            public void lerXML()
            {
                XmlTextReader reader = new XmlTextReader(_xmlfile);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        _node = reader.Name.ToCharArray();
                        if (reader.HasAttributes)
                        {
                            _node = reader.Name.ToCharArray();
                            while (reader.MoveToNextAttribute())
                            {
                                _attr = reader.Name.ToCharArray();
                                _attrVal = reader.Value.ToCharArray();
                            }
                            reader.MoveToElement();
                        }
                    }
    
                }
            }

    And in main program

     Console.WriteLine(configXML.getnodes);
     Console.WriteLine(configXML.getattr);
     Console.WriteLine(configXML.getattrVal);
    Could you help pls
    Thank you,
    Friday, January 11, 2013 11:28 AM

Answers

  • Got it.

    configXML

            public void lerXML()
            {
                XmlTextReader reader = new XmlTextReader(_xmlfile);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                reader.Read();
                reader.MoveToElement();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.HasAttributes)
                    {
                        _elNome.Add(reader.Name);
                        while (reader.MoveToNextAttribute())
                        {
                            _attrNome.Add(reader.Name);
                            _attrVal.Add(reader.Value);
                        }
                        reader.MoveToElement();
                    }
                    _countList = _elNome.Count;
                }
            }

    Main program

    for (int i = 0; i < configXML.countList; i++)
    {
    Console.WriteLine(configXML.elNome[i] + " : " + configXML.getattrName[i] + " : " + configXML.getattrVal[i]);
    }

    Thank you anyway.

    Friday, January 11, 2013 2:15 PM

All replies

  • Got it.

    configXML

            public void lerXML()
            {
                XmlTextReader reader = new XmlTextReader(_xmlfile);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                reader.Read();
                reader.MoveToElement();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.HasAttributes)
                    {
                        _elNome.Add(reader.Name);
                        while (reader.MoveToNextAttribute())
                        {
                            _attrNome.Add(reader.Name);
                            _attrVal.Add(reader.Value);
                        }
                        reader.MoveToElement();
                    }
                    _countList = _elNome.Count;
                }
            }

    Main program

    for (int i = 0; i < configXML.countList; i++)
    {
    Console.WriteLine(configXML.elNome[i] + " : " + configXML.getattrName[i] + " : " + configXML.getattrVal[i]);
    }

    Thank you anyway.

    Friday, January 11, 2013 2:15 PM
  • Thanks for sharing solution here.

    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 14, 2013 6:35 AM
    Moderator