0

I have an xml string looking something like this

<row Name="analog.__VG_SPP3_SFRTPCT" />
<row Name="analog._3305_LIST210_1" />
<row Name="analog._AG_5340_PR14AN" />
<row Name="analog._AG_EPNT_2" />
<row Name="analog._AG_EPNT_SP" />
<row Name="analog._AG_MERC_ERXTES" />
<row Name="analog._AG_ROC_TEST" />
<row Name="analog._AG_ROM1_LOAD" />
<row Name="analog._AG_TEST_CRC1LT" />
<row Name="analog._AG_TEST_CRC1RT" />
<row Name="analog._CWAV_TST_MDP1CV" />
<row Name="analog._CWAV_TST_MDP1CV_LIST" />

Trying to generate an option list for a combo box displayed in javascript, I am trying to loop through the xml string in razor to generate it, however I am not quite sure how to or if its even possible, I can do this by splitting the xml into a separate data structure and then looping through that datastructure again in razor. However for performance reasons it would be nice to be able to do this in one go.

@model string
@{
    Response.ContentType = "text/xml";
    Layout = null;
}
<complete>
    <option value=""><![CDATA[&nbsp;]]></option>
    @foreach (var row  in @Model)
    {
        <option value="@(row.Name)">@(row.Name)</option>  
    }
</complete>
4
  • i have no idea, but could you ajax it back to the controller to do? Commented Jun 12, 2013 at 20:21
  • I guess I could potentially do this in the controller and just have razor pass the string along unchanged back to the ajax caller on the javascript side. Commented Jun 12, 2013 at 20:25
  • You could change the controller to give you an IEnumerable<string> with each entry being a row instead of all the rows in a single string. Commented Jun 12, 2013 at 20:42
  • you could convert the row to XElement and then fetch the attributes value very easy and output it with razor: foreach (...) var xmlElement = XElement.Parse(row); var name = xmlElement.Attribute("Name").Value; ...@name... Commented Jun 12, 2013 at 20:52

1 Answer 1

1

For completeness, here is what I ended up doing in razor, thanks pasty. I encapsulated the output xml string with on the controller side, then inserted it in a ComboData structure, apparently razor had issues with a pure string, when the data got too large.

controller:

var Names = "<rows>" + oService.Names(ID) + "</rows>";
var oComboData = new ComboData(Names);
return View("ComboData", oComboData);

viewmodel:

public class ComboData
{
    public ComboData(string sString)
    {
        xdoc = new XmlDocument();
        xdoc.LoadXml(sString);
    }

    public XmlDocument xdoc { get; set; }
}

view:

@using System.Xml
@using ViewModels; 
@model ComboData
@{
    Response.ContentType = "text/xml";
    Layout = null;
}
<complete>
    <option value=""><![CDATA[&nbsp;]]></option>
    @foreach (XmlNode node in Model.xdoc.DocumentElement.ChildNodes)
    {
        <option value="@(node.Attributes[0].Value)">@(node.Attributes[0].Value)</option>
    }
</complete>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.