简体   繁体   中英

How to get attributes from a XML file?

I have .xml like this:

<Type>
  <Connections>
    <Conn ServerName="serv1" DataBase="Persons" User="admin" Pass="123"/>
    <Conn ServerName="serv2" DataBase="Type123" User="admin" Pass="123"/>
  </Connections>
  <UDLFiles>
    <UDL Path="C:\Test.UDL>
  </UDLFiles>
</Type> 

and I want to save all the ServerNames in string[] ServerNames; all the DataBases in string[] DataBases; and so on... How to get all these Atributes of this XML? I try it with:

XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(path);
        XmlNodeList conn = xmlDoc.GetElementsByTagName("Conn");
        foreach (XmlNode item in conn)
        {
            //item.Attributes;
        }

but I cant get Atributes with this code... Any ideas?

Cast to XmlElement instead of XmlNode. Then you can use the Attributes property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM