2012-11-08 77 views
2

我試圖將XML字符串轉換爲XML對象以遍歷其屬性和子節點。從ScriptSharp讀取XML字符串


像這樣的事情經常在C#: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

然而腳本#對System.Xml庫比NET的不同,我不能實例化一個新的XmlDocument(沒有公共構造函數)。


或像這樣使用jQuery: How to parse XML using jQuery?

但是我不知道該怎麼稱呼中ScriptSharp的jQuery的非靜態ParseXml(串)的功能。

編輯:

得到了ParseXml功能一起工作:

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

但我得到的WebKit錯誤:

$ P1:「對象功能(A, b){return new c.fn.init(a,b)}沒有方法 'parseXml''


有什麼想法?

回答

0

得到它的工作的最簡單的方法:)

根據這個帖子: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

1

如此簡單:

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