<tfoot id='tLdJz'></tfoot>

    <legend id='tLdJz'><style id='tLdJz'><dir id='tLdJz'><q id='tLdJz'></q></dir></style></legend>

      <bdo id='tLdJz'></bdo><ul id='tLdJz'></ul>
  1. <small id='tLdJz'></small><noframes id='tLdJz'>

  2. <i id='tLdJz'><tr id='tLdJz'><dt id='tLdJz'><q id='tLdJz'><span id='tLdJz'><b id='tLdJz'><form id='tLdJz'><ins id='tLdJz'></ins><ul id='tLdJz'></ul><sub id='tLdJz'></sub></form><legend id='tLdJz'></legend><bdo id='tLdJz'><pre id='tLdJz'><center id='tLdJz'></center></pre></bdo></b><th id='tLdJz'></th></span></q></dt></tr></i><div id='tLdJz'><tfoot id='tLdJz'></tfoot><dl id='tLdJz'><fieldset id='tLdJz'></fieldset></dl></div>

      读取 XML(从字符串)并获取一些字段 - 读取 XML 时出

      时间:2023-08-28
      <legend id='OIa8H'><style id='OIa8H'><dir id='OIa8H'><q id='OIa8H'></q></dir></style></legend>
        <tbody id='OIa8H'></tbody>

      <small id='OIa8H'></small><noframes id='OIa8H'>

            <i id='OIa8H'><tr id='OIa8H'><dt id='OIa8H'><q id='OIa8H'><span id='OIa8H'><b id='OIa8H'><form id='OIa8H'><ins id='OIa8H'></ins><ul id='OIa8H'></ul><sub id='OIa8H'></sub></form><legend id='OIa8H'></legend><bdo id='OIa8H'><pre id='OIa8H'><center id='OIa8H'></center></pre></bdo></b><th id='OIa8H'></th></span></q></dt></tr></i><div id='OIa8H'><tfoot id='OIa8H'></tfoot><dl id='OIa8H'><fieldset id='OIa8H'></fieldset></dl></div>

            • <tfoot id='OIa8H'></tfoot>

                <bdo id='OIa8H'></bdo><ul id='OIa8H'></ul>
                本文介绍了读取 XML(从字符串)并获取一些字段 - 读取 XML 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有这个 XML(存储在一个名为 myXML 的 C# 字符串中)

                I have this XML (stored in a C# string called myXML)

                <?xml version="1.0" encoding="utf-16"?>
                <myDataz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                  <listS>
                    <sog>
                      <field1>123</field1>
                      <field2>a</field2>
                      <field3>b</field3>
                    </sog>
                    <sog>
                      <field1>456</field1>
                      <field2>c</field2>
                      <field3>d</field3>
                    </sog>
                  </listS>
                </myDataz>
                

                我想浏览所有 <sog> 元素.对于他们每个人,我想打印孩子 <field1>.

                and I'd like to browse all <sog> elements. For each of them, I'd like to print the child <field1>.

                这是我的代码:

                XmlDocument xmlDoc = new XmlDocument();
                string myXML = "<?xml version="1.0" encoding="utf-16"?><myDataz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><listS><sog><field1>123</field1><field2>a</field2><field3>b</field3></sog><sog><field1>456</field1><field2>c</field2><field3>d</field3></sog></listS></myDataz>"
                xmlDoc.Load(myXML);
                XmlNodeList parentNode = xmlDoc.GetElementsByTagName("listS");
                foreach (XmlNode childrenNode in parentNode)
                {
                    HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//field1").Value);
                }
                

                但似乎我无法将字符串读取为 XML?我得到 System.ArgumentException

                but seems I can't read a string as XML? I get System.ArgumentException

                推荐答案

                你应该使用 LoadXml 方法,而不是 Load:

                You should use LoadXml method, not Load:

                xmlDoc.LoadXml(myXML); 
                

                Load 方法试图从文件中加载 xml 并从字符串中加载 LoadXml.你也可以使用 XPath:

                Load method is trying to load xml from a file and LoadXml from a string. You could also use XPath:

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);
                
                string xpath = "myDataz/listS/sog";
                var nodes = xmlDoc.SelectNodes(xpath);
                
                foreach (XmlNode childrenNode in nodes)
                {
                    HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//field1").Value);
                } 
                

                这篇关于读取 XML(从字符串)并获取一些字段 - 读取 XML 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 .net 中读取大型 XML 文档 下一篇:C# 从 XML 中提取数据

                相关文章

                最新文章

              • <legend id='szGm5'><style id='szGm5'><dir id='szGm5'><q id='szGm5'></q></dir></style></legend>

                  <i id='szGm5'><tr id='szGm5'><dt id='szGm5'><q id='szGm5'><span id='szGm5'><b id='szGm5'><form id='szGm5'><ins id='szGm5'></ins><ul id='szGm5'></ul><sub id='szGm5'></sub></form><legend id='szGm5'></legend><bdo id='szGm5'><pre id='szGm5'><center id='szGm5'></center></pre></bdo></b><th id='szGm5'></th></span></q></dt></tr></i><div id='szGm5'><tfoot id='szGm5'></tfoot><dl id='szGm5'><fieldset id='szGm5'></fieldset></dl></div>
                    <bdo id='szGm5'></bdo><ul id='szGm5'></ul>

                  <tfoot id='szGm5'></tfoot>

                  <small id='szGm5'></small><noframes id='szGm5'>