2009-06-01 21 views
67

Jak mogę odczytać atrybut XML za pomocą XmlDocument z C#?Czytanie atrybutu XML za pomocą XmlDocument

Mam plik XML, który wygląda mniej więcej tak:

<?xml version="1.0" encoding="utf-8" ?> 
<MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream"> 
    <Other stuff /> 
</MyConfiguration> 

Jak czytam XML atrybuty SuperNumber i superstrun?

Obecnie używam XmlDocument, i otrzymuję wartości pomiędzy za pomocą XmlDocument's GetElementsByTagName() i to działa bardzo dobrze. Po prostu nie mogę wymyślić, jak uzyskać atrybuty?

Odpowiedz

96
XmlNodeList elemList = doc.GetElementsByTagName(...); 
for (int i = 0; i < elemList.Count; i++) 
{ 
    string attrVal = elemList[i].Attributes["SuperString"].Value; 
} 
+0

bardzo dziękuję. to naprawdę działa i nie potrzebuje żadnych ścieżek ani niczego. po prostu wspaniałe !! – Nani

5

XmlDocument.Attributes być może? (Który ma metoda A GetNamedItem która przypuszczalnie robić, co chcesz, chociaż ja zawsze tylko powtórzyć zbiór atrybutów)

81

Należy spojrzeć na XPath. Gdy zaczniesz go używać, okaże się, że jego kodowanie jest o wiele wydajniejsze i łatwiejsze do kodowania niż przeglądanie list. Pozwala także bezpośrednio uzyskać to, co chcesz.

Następnie kod byłoby coś podobnego do

string attrVal = doc.SelectSingleNode("/MyConfiguration/@SuperNumber").Value; 
8

Można migrować do XDocument, zamiast XmlDocument, a następnie użyć LINQ jeśli wolisz składnię. Coś jak:

var q = (from myConfig in xDoc.Elements("MyConfiguration") 
     select myConfig.Attribute("SuperString").Value) 
     .First(); 
5

Mam Books.xml pliku XML

<ParameterDBConfig> 
    <ID Definition="1" /> 
</ParameterDBConfig> 

Program:

XmlDocument doc = new XmlDocument(); 
doc.Load("D:/siva/books.xml"); 
XmlNodeList elemList = doc.GetElementsByTagName("ID");  
for (int i = 0; i < elemList.Count; i++)  
{ 
    string attrVal = elemList[i].Attributes["Definition"].Value; 
} 

Teraz attrVal ma wartość ID.

1

Zakładając Twój przykład dokument jest w ciągu zmiennej doc

> XDocument.Parse(doc).Root.Attribute("SuperNumber") 
1 
0

Jeśli XML zawiera nazw, można wykonać następujące czynności w celu uzyskania wartości atrybutu:

var xmlDoc = new XmlDocument(); 

// content is your XML as string 
xmlDoc.LoadXml(content); 

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); 

// make sure the namespace identifier, URN in this case, matches what you have in your XML 
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol"); 

// get the value of Destination attribute from within the Response node with a prefix who's identifier is "urn:oasis:names:tc:SAML:2.0:protocol" using XPath 
var str = xmlDoc.SelectSingleNode("/ns:Response/@Destination", nsmgr); 
if (str != null) 
{ 
    Console.WriteLine(str.Value); 
} 

Więcej o przestrzeniach nazw XML here i here.

0

Robiłem to:

XmlDocument d = new XmlDocument(); 
d.Load("http://your.url.here"); 
List<string> items = new List<string>(); 

foreach (XmlAttribute attr in d.DocumentElement.Attributes) 
{ 
    items.Add(attr.LocalName);     
} 
Powiązane problemy