所以我有由网络设备生成的.xml文件。 .xml看起来像这样:
<xml>
<messages>
<item>
<UpdatedInDB> 2019-01-14 15:25:05 < /UpdatedInDB>
<ReceivingDateTime>2018-10-22 09:53:51</ReceivingDateTime>
<Text>004B0075006C0074007500720061</Text>
<SenderNumber>+420000000000</SenderNumber>
<Coding>Default_No_Compression</Coding>
<UDH/>
<SMSCNumber>+420000000000</SMSCNumber>
<Class>-1</Class>
<TextDecoded>Kultura</TextDecoded>
<ID>29</ID>
<RecipientID></RecipientID>
<Processed>t</Processed>
<id_folder>1</id_folder>
<readed>true</readed>
<oid/>
<Status>0</Status>
</item>
<item>
<UpdatedInDB> 2019-01-14 15:25:05 < /UpdatedInDB>
<ReceivingDateTime>2018-10-22 09:53:51</ReceivingDateTime>
<Text>004B0075006C0074007500720061</Text>
<SenderNumber>+420000000000</SenderNumber>
<Coding>Default_No_Compression</Coding>
<UDH/>
<SMSCNumber>+420000000000</SMSCNumber>
<Class>-1</Class>
<TextDecoded>Kultura</TextDecoded>
<ID>29</ID>
<RecipientID></RecipientID>
<Processed>t</Processed>
<id_folder>1</id_folder>
<readed>true</readed>
<oid/>
<Status>0</Status>
</item>
</message>
</xml>
如何从每个节点获取SenderNumber和textDecoded?
我设法使用此方法仅从第一个节点获取数据
{
string baseUrl = "http://192.168.253.160/index.php/http_api/read_sms";
WebClient client = new WebClient();
client.QueryString.Add("login", "admin");
client.QueryString.Add("pass", "password");
client.QueryString.Add("folder", "inbox");
client.QueryString.Add("responsetype", "xml");
client.QueryString.Add("unread", "1");
/*client.QueryString.Add("to", cislo);
client.QueryString.Add("message", text);*/
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
Stream receivedStream = client.OpenRead(baseUrl);
StreamReader reader = new StreamReader(receivedStream);
string result = reader.ReadToEnd();
receivedStream.Close();
reader.Close();
//parsing
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
string cislo = "0";
string zprava = "0";
XmlNodeList number = doc.GetElementsByTagName("SenderNumber");
for (int i = 0; i < number.Count; i++)
{
cislo = number[i].InnerXml;
}
XmlNodeList text = doc.GetElementsByTagName("TextDecoded");
for (int i = 0; i < text.Count; i++)
{
zprava = text[i].InnerXml;
}
MessageBox.Show(cislo);
MessageBox.Show(zprava);
}
问题是我仅从api读取未读消息。如果我只读取第一个节点,我将丢失其余的节点,而第二次将不会在那里,因为它们已经被“读取”了。
有人可以给我建议吗?我当时尝试使用Google,但并未真正找到对我有用的东西。
非常感谢。
答案 0 :(得分:1)
在for
循环中,您总是替换变量的值(cislo
或zprava
),因此最后,您只会看到已解析的最后一个值。如果要跟踪它们,可以将它们存储在列表或数组中,也可以将它们直接附加到字符串中。例如,
var cislo = new List<string>();
var zprava = new List<string>();
XmlNodeList number = doc.GetElementsByTagName("SenderNumber");
for (int i = 0; i < number.Count; i++)
{
cislo.Add(number[i].InnerXml);
}
XmlNodeList text = doc.GetElementsByTagName("TextDecoded");
for (int i = 0; i < text.Count; i++)
{
zprava.Add(text[i].InnerXml);
}
MessageBox.Show(string.Join(", ", cislo));
MessageBox.Show(string.Join(", ", zprava));
答案 1 :(得分:0)
您可以使用XElement来获取数据并将其打包到anonymous type中:
var xml = XElement.Parse(result);
var data = xml
.Elements()
.Select(e => new { SenderNumber = e.Element("SenderNumber").Value,
TextDecoded = e.Element("TextDecoded").Value });
// iterate over collection
data.ToList().ForEach(e => Console.WriteLine($"Sender: {e.SenderNumber}, " +
$"TextDecoded: {e.TextDecoded}"));