Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#(Xamarin):在XML中循环_C#_Xml_Xamarin - Fatal编程技术网

C#(Xamarin):在XML中循环

C#(Xamarin):在XML中循环,c#,xml,xamarin,C#,Xml,Xamarin,我有一个Xamarin(C#)项目,我试图循环使用一些XML,但由于某些原因,我的代码无法工作 这就是我现在拥有的: DeviceList = new List<DeviceInfo>(); string ResultStatus = ""; string ResultDevice = ""; var result = Encoding.Default.GetString(e.Result); result = "&l

我有一个Xamarin(C#)项目,我试图循环使用一些XML,但由于某些原因,我的代码无法工作

这就是我现在拥有的:

DeviceList = new List<DeviceInfo>();
        string ResultStatus = "";
        string ResultDevice = "";
        var result = Encoding.Default.GetString(e.Result);

        result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + result;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(result);

        string xPathStatus = "ed_listdevices";
        var nodes = xmlDoc.SelectNodes(xPathStatus);
        foreach (XmlNode xNode in nodes) {
            ResultStatus = xNode.SelectSingleNode("//status").InnerText;
            ResultDevice = xNode.SelectSingleNode("//device").InnerText;
        }

        if (ResultStatus.ToLower() == "ok") {
            XmlDocument deviceDoc = new XmlDocument();
            deviceDoc.LoadXml(result);
            var deviceNodes = deviceDoc.SelectNodes(xPathStatus + "/device");
            //foreach(XmlNode dNode in deviceNodes) {
            for (int i = 0; i < deviceNodes.Count; i++) {
                DeviceList.Add(new DeviceInfo() {
                    DeviceID = deviceNodes[i].SelectSingleNode("//id").InnerXml,
                    DeviceName = deviceNodes[i].SelectSingleNode("//name").InnerXml,
                    DeviceExtraName = "",
                    DeviceOnlineStatus = deviceNodes[i].SelectSingleNode("//status").InnerXml,
                    Location = deviceNodes[i].SelectSingleNode("//address").InnerXml,
                    Time = deviceNodes[i].SelectSingleNode("//time").InnerXml
                });
            }
DeviceList=newlist();
字符串ResultStatus=“”;
字符串ResultDevice=“”;
var result=Encoding.Default.GetString(e.result);
result=”“+结果;
XmlDocument xmlDoc=新的XmlDocument();
LoadXml(结果);
字符串xPathStatus=“ed_listdevices”;
var nodes=xmlDoc.SelectNodes(xPathStatus);
foreach(XmlNode中的xNode){
ResultStatus=xNode.SelectSingleNode(//状态”).InnerText;
ResultDevice=xNode.SelectSingleNode(//设备”).InnerText;
}
if(ResultStatus.ToLower()=“确定”){
XmlDocument deviceDoc=新的XmlDocument();
加载xml(结果);
var deviceNodes=deviceDoc.SelectNodes(xPathStatus+“/device”);
//foreach(设备节点中的XmlNode){
对于(int i=0;i
}

当我逐步浏览代码时,我正确地得到了“ResultStatus”和“ResultDevice”,当我到达
for(int i=0;i
“deviceNodes”变量的计数为91,我可以看到我从调用的Web服务中获得的所有xml元素

然而,当我在deviceNodes[I]中循环时,我只从第一个XML元素中获取值(是的,“I”的值确实发生了变化)。换句话说,我的DeviceList中填充了91个具有相同值的条目

我是否遗漏了一些明显的东西?既然这不起作用,我做错了什么


PS:我也尝试过使用
foreach(deviceNodes中的XmlNode节点)
,但结果是一样的。

“/”选择器告诉它从文档的根节点进行搜索。如果要在“当前”节点下进行本地搜索,请删除“/”

“/”在中,选择器告诉它从文档的根节点进行搜索。如果您想在“当前”节点下进行本地搜索,请删除“/”

当文件的大小很小时,您的xml有多大(我们主要是在大小不超过几兆字节的情况下进行搜索),将符合您的模式的类放在一起并将xml反序列化到POCO并使用它们几乎总是比较容易的。我相信,在选择器中使用“/”语法会告诉它从根目录搜索,而不是从声明为静态的本地节点搜索?@Jason您得到了:-)删除“/”是的!如果你把它放在一个答案中,我会接受它作为正确的答案。谢谢!你的xml有多大?当文件的大小很小时(我们主要是用几兆字节的大小来做),几乎总是很容易把坚持你的模式的类放在一起,将xml反序列化到POCO并使用它们。我相信使用“/”选择器中的语法告诉它从根目录搜索,而不是从声明为静态的本地nodeIs DeviceInfo()进行搜索?@Jason你知道了:-)删除“/”成功了!如果你把它放在答案中,我会接受它作为正确答案。谢谢!