2014-01-09 37 views
0

我想要做的事,如:的XMLReader,得到屬性的名稱

stringBuilder.AppendLine(" globalVar." + reader.GetAttribute(i).Name + " = " + reader[i] + "; //add param "); 

其中 「reader.GetAttribute(I),請將.Name」 是不工作的組成部分。有沒有一種等同的方法來獲取屬性的名稱?

+0

看看[MSDN - 閱讀屬性](http://msdn.microsoft.com/en-us/library/by2bd43b%28v=vs.110%29.aspx) –

回答

2

使用reader.MoveToAttribute(i),那麼你可以使用reader.Namereader.Value

+0

謝謝這最終確實工作...我必須改變一些邏輯,但它確實如此。 – yoyo

0

使用NameValue直接在讀取器 - 見試樣從下方Reading Attributes

if (reader.HasAttributes) { 
    Console.WriteLine("Attributes of <" + reader.Name + ">"); 
    while (reader.MoveToNextAttribute()) { 
    Console.WriteLine(" {0}={1}", reader.Name, reader.Value); 
    } 
    // Move the reader back to the element node. 
    reader.MoveToElement(); 
} 
0

的getAttribute()返回的屬性的字符串值。它沒有「名稱」屬性。嘗試移動到該屬性,然後獲取「名稱」屬性。

+0

你是什麼意思'移動屬性'?我知道GetAttribute()返回屬性的字符串值...我問是否有辦法獲得'ids',如果你願意每個屬性。 – yoyo

+0

我看到約翰有上面的答案。讀者。移至屬性(i)。他們的讀者。名字應該這樣做。 – AnthonyBCodes