[C#][Tips][File] XML:属性値リスト

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void mRead_SelectNodes_Attrs()
{
    List<string>  slValue = new List<string> ();
 
    /* File */
    var xDoc = new System.Xml.XmlDocument();
    xDoc.Load(@"D:\CustomersOrdwes.xml");
 
    /* Node(List) */
    string sXPath = @"/Root/Customers/Customer";
    System.Xml.XmlNodeList xNodeList = xDoc.SelectNodes(sXPath);
 
    /* Element-Attribute */
    string sAttrName = "CustomerID";
    foreach (System.Xml.XmlNode item in xNodeList)
    {
        foreach (System.Xml.XmlNode item_Attr in item.Attributes)
        {
            if (item_Attr.Name == sAttrName) slValue.Add((string)item_Attr.Value);
        }
 
    }
 
    /*  */
    slValue.ForEach(c => Console.WriteLine(c));
 
}