【问题标题】:Read XML string from ScriptSharp从 ScriptSharp 读取 XML 字符串
【发布时间】:2012-11-08 15:04:49
【问题描述】:

我正在尝试将 XML 字符串转换为 XML 对象,以便遍历其属性和子节点。


在常规 C# 中是这样的: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

但是,Script# 的 System.Xml 库与 .Net 的不同,我无法实例化新的 XmlDocument(没有公共构造函数)。


或者像这样使用 jQuery: How to parse XML using jQuery?

但是我不知道如何在 ScriptSharp 的 jQuery 中调用非静态 ParseXml(string) 函数。

编辑:

使用 ParseXml 函数:

XmlDocument doc = jQuery.Instance.ParseXml(objDictionaryItem.Body);

但我在 WebKit 中遇到错误:

$p1: "对象函数 (a,b){return new c.fn.init(a,b)} 没有方法 'parseXml'"


有什么想法吗?

【问题讨论】:

    标签: jquery xml script# parsexml


    【解决方案1】:

    就这么简单:

    XmlDocument xmlDoc = XmlDocumentParser.Parse(@"<note>
                                                    <to>Tove</to>
                                                    <from>Jani</from>
                                                    <heading>Reminder</heading>
                                                    <body>Don't forget me this weekend!</body>
                                                   </note>");
    

    【讨论】:

      【解决方案2】:

      用最简单的方式让它工作:)

      根据这篇文章:https://stackoverflow.com/a/649655/1809564 我可以将简单的 jQueryObject (var) 视为 XML 文件,只要它格式正确。

      因此,如果我的 objDictionaryItem.Body 变量是格式良好的 XML 字符串,如下所示:

      <ItemBody>
          <Key>132515840</Key>
          <Revision>1</Revision>
          <Name>Line</Name>
          <TestParameters>
              <TestParameterKey Key="132646912" DisplayString="Success">Success</TestParameterKey>
              <TestParameterKey Key="132646913" DisplayString="Downstrea">Downstream</TestParameterKey>
              <TestParameterKey Key="132646914" DisplayString="Upstream">Upstream</TestParameterKey>
              <TestParameterKey Key="132646915" DisplayString="Attenuation">Attenuation</TestParameterKey>
          </TestParameters>
      </ItemBody>
      

      我可以简单地使用它来解析它:

      jQueryObject xmlDoc = jQuery.FromObject(objDictionaryItem.Body);
      Script.Alert(xmlDoc.Find("Revision").GetText());
      

      并使用此处描述的相同 jQuery 方法:http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-14
        • 1970-01-01
        • 2019-04-14
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多