Display XML (in formatted way) into a Razor Textar

2019-04-12 09:10发布

问题:

i have a little problem. I have a string (containing XML code). Example:

<PosizioneRischio>
 <ParametriInput>
  <Utente>Utente</Utente>
<Input>
  <ClienteRichiesto>
    <CodiceCR>1</CodiceCR>
    <CodiceCRA />
  </ClienteRichiesto>
  <IstitutiRichiesti>
    <IstitutoRichiesto>1</IstitutoRichiesto>
  </IstitutiRichiesti>
  <DateContabiliRichieste>
    <DataContabileRichiesta>2014</DataContabileRichiesta>
    </DateContabiliRichieste>
 </Input>
 <ScalaImporti>5000</ScalaImporti>
</ParametriInput>
<PosizioneRischio>

I would like to show it in a textarea with razor, in a formatted way.

Supponing the string name is "xmlstring", i would like doing something like:

@Html.TextArea("Info",xmlstring, new {cols="105", rows="30",@readonly="readonly",@disabled="disabled" })</td>

But in this manner, xml code appears flattened.. obvioulsy the result is not formatted. How can i make it formatted? Sorry for the question..i'm a newbie

回答1:

var xml = "<root><A><B>0</B><C>0</C></A><D><E>0</E></D></root>";

XDocument doc = XDocument.Parse(xml);
Console.WriteLine (doc.ToString());

will print:

<root>
  <A>
    <B>0</B>
    <C>0</C>
  </A>
  <D>
    <E>0</E>
  </D>
</root>

So you need to do

@Html.TextArea("Info", XDocument.Parse(xmlstring).ToString(), new {cols="105", rows="30", @readonly="readonly", @disabled="disabled" })</td>