2017-02-03 66 views
5

Bazı XML verilerini bir web sunucusundan döndürmek için bir API çağrısı kullanıyorum. XML verileri aşağıdaki biçimdedir:C# - StreamColor sonuçtan XML Düğüm değerleri Sting olarak ayarlama

<forismatic> 
    <quote> 
     <quoteText>The time you think you're missing, misses you too.</quoteText>    
     <quoteAuthor>Ymber Delecto</quoteAuthor> 
     <senderName></senderName> 
     <senderLink></senderLink> 
     <quoteLink>http://forismatic.com/en/55ed9a13c0/</quoteLink> 
    </quote> 
</forismatic> 

başarıyla ham XML verilerini almak, ve ben dizelere <quoteText> ve <quoteAuthor> düğüm değerleri eklemek istiyorum ama bunu yapmak mümkün görünmektedir. Benim geçerli kod: dize değeri quote ayarlamaya çalışırken tip 'System.NullReferenceException' türünde işlenmeyen bir özel durum ile

private void btnGetQuote_Click(object sender, EventArgs e) 
    { 
     WebRequest req = WebRequest.Create("http://api.forismatic.com/api/1.0/");        
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 

     string reqString = "method=getQuote&key=457653&format=xml&lang=en"; 
     byte[] reqData = Encoding.UTF8.GetBytes(reqString); 
     req.ContentLength = reqData.Length; 

     using (Stream reqStream = req.GetRequestStream()) 
      reqStream.Write(reqData, 0, reqData.Length); 

     using (WebResponse res = req.GetResponse()) 
     using (Stream resSteam = res.GetResponseStream()) 
     using (StreamReader sr = new StreamReader(resSteam)) 
     { 
      string xmlData = sr.ReadToEnd(); 
      txtXmlData.Text = xmlData; 
      Read(xmlData); 
     } 
    } 

    private void Read(string xmlData) 
    { 
     XDocument doc = XDocument.Parse(xmlData); 
     string quote = doc.Element("quote").Attribute("quoteText").Value; 
     string auth = doc.Element("quote").Attribute("quoteAuthor").Value; 
     txtQuoteResult.Text = "QUOTE: " + quote + "\r\n" + "AUTHOR: " + auth;      
    } 

Programım bombaları oluştu. Benzer bazı yayınlara baktım ve çeşitli değişiklikler yaptım, ancak ayarlanan iki dizgi değerini elde edemiyorum.

cevap

5

doc.Element("quote") kullanmayı deniyorsunuz - böyle bir öğe yok, bu yüzden boşa dönüyor. doc.Root.Element("quote")'u istersiniz. Ardından, quoteText ve quoteAuthor için özniteliklermiş gibi soruyorsunuz - onlar değil, onlar da öğelerdir.

private void Read(string xmlData) 
{ 
    XDocument doc = XDocument.Parse(xmlData); 
    XElement quote = doc.Root.Element("quote"); 
    string text = quote.Element("quoteText").Value; 
    string author = quote.Element("quoteAuthor").Value; 
    txtQuoteResult.Text = $"QUOTE: {text}\r\nAUTHOR: {author}"; 
} 

(Ben şahsen yöntem dize değeri dönmek ve arama yöntemi içinde txtQuoteResult.Text olarak ayarlamak yapmak istiyorum, ama bu farklı bir konu.)

+0

:

Yani temelde istediğiniz Bu harika, tanklar çok fazla! Bir test yaptı ve bir tedavi çalışması yapıyor. Hala XML'le uğraşmak ve düğümleri/öznitelikleri/öğeleri çalışmak. :) – Rawns

İlgili konular