VB.NET, C#, PowerShellを使用しているエンジニアのためのサイト。
基本的な使用方法から開発で役立つTipsまで幅広く取り扱っています。
指定した要素の属性を取得するには、要素のGetAttributesプロパティを使用します。このプロパティがTrueの場合は子要素があることを示します。
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); // XMLファイルを読み込む xmlDoc.Load(@"C:\Work\Test.xml"); //ルート要素を取得する System.Xml.XmlElement rootElement = xmlDoc.DocumentElement; //rootElementが属性を持っているか? if ( rootElement.HasAttributes == true ) //属性を取得する Console.WriteLine(rootElement.GetAttribute("zip")); else Console.WriteLine("属性はありません"); Console.WriteLine(rootElement.GetAttribute("zip"));