C# - 将 XML 节点值设置为来自 StreamReader 结果的 Stings
C# - Setting XML Node values as Stings from StreamReader result
我正在使用 API 调用来自网络服务器的 return 一些 XML 数据。 XML 数据采用以下格式:
<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>
我可以成功检索原始 XML 数据,我想将 <quoteText>
和 <quoteAuthor>
节点值添加到字符串,但似乎无法执行此操作。我当前的代码:
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;
}
我的程序因 在尝试设置字符串值 quote
时发生 'System.NullReferenceException' 类型的未处理异常 而崩溃。我查看了一些类似的帖子并进行了各种更改,但似乎无法设置两个字符串值。
您正在尝试使用 doc.Element("quote")
- 没有这样的元素,因此 returning 为 null。你会想要 doc.Root.Element("quote")
。接下来你要求 quoteText
和 quoteAuthor
就好像它们是属性一样 - 它们不是,它们也是元素。
所以基本上你想要:
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}";
}
(我个人将方法 return 设置为字符串值,并在调用方法中将其设置为 txtQuoteResult.Text
,但这是另一回事.)
我正在使用 API 调用来自网络服务器的 return 一些 XML 数据。 XML 数据采用以下格式:
<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>
我可以成功检索原始 XML 数据,我想将 <quoteText>
和 <quoteAuthor>
节点值添加到字符串,但似乎无法执行此操作。我当前的代码:
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;
}
我的程序因 在尝试设置字符串值 quote
时发生 'System.NullReferenceException' 类型的未处理异常 而崩溃。我查看了一些类似的帖子并进行了各种更改,但似乎无法设置两个字符串值。
您正在尝试使用 doc.Element("quote")
- 没有这样的元素,因此 returning 为 null。你会想要 doc.Root.Element("quote")
。接下来你要求 quoteText
和 quoteAuthor
就好像它们是属性一样 - 它们不是,它们也是元素。
所以基本上你想要:
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}";
}
(我个人将方法 return 设置为字符串值,并在调用方法中将其设置为 txtQuoteResult.Text
,但这是另一回事.)