mvc 2 帮助处理 xml 部分

发布于 2024-09-16 13:09:54 字数 1622 浏览 7 评论 0原文

好的,为了向 Fusion Charts 提供数据,我需要给它一些 XML。 我已经将其范围缩小到了

myChart.setDataXML("<%Html.RenderPartial("Graph", Model.graph_data); %>");

渲染的范围,

myChart.setDataXML("
    <chart caption='Grafico' xAxisName='Factores' yAxisName='Porcentaje' decimals='0' formatNumberScale='0' numberSuffix='%25' yAxisMinValue='0' yAxisMaxValue='100' bgColor='FFFFFF' showBorder='1' bgSWF='/Content/images/LogoGraficas.png' slantLabels='1' labelDisplay='Rotate' baseFontColor='333333'>

<set value='100' label='Pierna' />
        <styles>
            <definition>
                <style name='myShadow' type='Shadow' color='999999' angle='45'/>
            </definition>
            <application>
                <apply toObject='DataValues' styles='myShadow' />
            </application>
        </styles>
    </chart>");

但我需要渲染的是..

myChart.setDataXML("<chart caption='Grafico' xAxisName='Factores' yAxisName='Porcentaje' decimals='0' formatNumberScale='0' numberSuffix='%25' yAxisMinValue='0' yAxisMaxValue='100' bgColor='FFFFFF' showBorder='1' bgSWF='/Content/images/LogoGraficas.png' slantLabels='1' labelDisplay='Rotate' baseFontColor='333333'><set value='100' label='Pierna' /><styles><definition><style name='myShadow' type='Shadow' color='999999' angle='45'/></definition><application><apply toObject='DataValues' styles='myShadow' /></application></styles></chart>");

如何使 RenderPartial 返回不带空格的字符串?

Ok, so in order to provide Data to Fusion Charts I need to give it some XML..
I have narrowed it down to this

myChart.setDataXML("<%Html.RenderPartial("Graph", Model.graph_data); %>");

which renders

myChart.setDataXML("
    <chart caption='Grafico' xAxisName='Factores' yAxisName='Porcentaje' decimals='0' formatNumberScale='0' numberSuffix='%25' yAxisMinValue='0' yAxisMaxValue='100' bgColor='FFFFFF' showBorder='1' bgSWF='/Content/images/LogoGraficas.png' slantLabels='1' labelDisplay='Rotate' baseFontColor='333333'>

<set value='100' label='Pierna' />
        <styles>
            <definition>
                <style name='myShadow' type='Shadow' color='999999' angle='45'/>
            </definition>
            <application>
                <apply toObject='DataValues' styles='myShadow' />
            </application>
        </styles>
    </chart>");

But what I need rendered is..

myChart.setDataXML("<chart caption='Grafico' xAxisName='Factores' yAxisName='Porcentaje' decimals='0' formatNumberScale='0' numberSuffix='%25' yAxisMinValue='0' yAxisMaxValue='100' bgColor='FFFFFF' showBorder='1' bgSWF='/Content/images/LogoGraficas.png' slantLabels='1' labelDisplay='Rotate' baseFontColor='333333'><set value='100' label='Pierna' /><styles><definition><style name='myShadow' type='Shadow' color='999999' angle='45'/></definition><application><apply toObject='DataValues' styles='myShadow' /></application></styles></chart>");

How can I make the RenderPartial give me back the string without the spaces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-09-23 13:09:54

您可以尝试 将 PartialView 输出转换为字符串 然后按照你的意愿操纵它。

所以你最终会做的是这样的。

string data = RenderPartialToString("~/..../..../..../Graph.ascx", Model.graph_data);
data.Replace(System.Environment.NewLine, "");
myChart.setDataXML(data);

我知道这不是最好的解决方案,但它可能会解决您的问题。

更新:

生成 XML 的更好方法是使用:XmlWriterXmlSerializerXDocument

我不是XML Expert,所以我不知道哪个最适合您的情况,但我喜欢 XmlSerializer 的工作方式。

请参阅此处的一些示例

You can try to convert the PartialView output to a string and then manipulate it as u wish.

So what you will end up doing is something like this.

string data = RenderPartialToString("~/..../..../..../Graph.ascx", Model.graph_data);
data.Replace(System.Environment.NewLine, "");
myChart.setDataXML(data);

I know it's not the best solution but it might fix your problem.

Update :

Better Ways to generate your XML is to use : XmlWriter or XmlSerializer or XDocument

I'm not an XML Expert so i don't know which is best for your case but i like the way the XmlSerializer works.

See some examples Here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文