我正在接受動態xml,我不知道屬性名稱,如果你看看xml和代碼......我試圖做一個簡單的例子,我可以獲得屬性值,即「myName」,「myNextAttribute」和「blah」,但我無法獲得屬性名稱,即「name」,「nextAttribute」和「etc1」。任何想法,我認爲它必須是我很想念的東西......但我肯定錯過了它。獲取屬性名稱除了xml中的屬性值
static void Main(string[] args)
{
string xml = "<test name=\"myName\" nextAttribute=\"myNextAttribute\" etc1=\"blah\"/>";
TextReader sr = new StringReader(xml);
using (XmlReader xr = XmlReader.Create(sr))
{
while (xr.Read())
{
switch (xr.NodeType)
{
case XmlNodeType.Element:
if (xr.HasAttributes)
{
for (int i = 0; i < xr.AttributeCount; i++)
{
System.Windows.Forms.MessageBox.Show(xr.GetAttribute(i));
}
}
break;
default:
break;
}
}
}
}
謝謝,我認爲它必須是密切的...我也在我原來的循環中找到了,我可以完成xr.MoveToAttribute(i)並獲得相同的效果。 – 2009-01-06 16:00:49