2

I am trying to convert a XML string to a XML object in order to traverse its attributes and child nodes.


Something like this in regular C#: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

However the System.Xml library of Script# is different than .Net's and I cannot instantiate a new XmlDocument (no public constructor).


Or like this with jQuery: How to parse XML using jQuery?

However I do not know how to call the non-static ParseXml(string) function in ScriptSharp's jQuery.

Edit:

Got the ParseXml function to work with:

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

But I get the error in WebKit:

$p1: "Object function (a,b){return new c.fn.init(a,b)} has no method 'parseXml'"


Any thoughts?

Community
  • 1
  • 1

2 Answers2

1

As simple as:

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

Got it working the simplest way :)

According to this post: https://stackoverflow.com/a/649655/1809564 I can treat a simple jQueryObject (var) as an XML file, as long as it is well formatted.

So if I my objDictionaryItem.Body variable is a well-formatted XML string like this:

<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>

I can parse it using simply:

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

And use the same jQuery methods described here: http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

Community
  • 1
  • 1