Home > Net >  Add <br/> to string in variable
Add <br/> to string in variable

Time:11-09

I want to show the XML schema on the browser page.

I need to add breakline <br/> to string to show info.

I tried like this:

<div>
    @XMLSchema
</div>
@code {

private string XMLSchema = "<?xml version='1.0'?>"  
        "<Employee>"  
            "<Employee>"  
                "<FirstName>First</FirstName>"  
                "<LastName>Second</LastName>"  
                "<ContactNo>1234567890</ContactNo>"  
                "<Email>[email protected]</Email>"  
                "<Address><br>"  
                    "<City>City</City>"  
                    "<Street>Street</Street>"  
                    "<StreetNr>1</StreetNr>"  
                    "<HouseNr>1</HouseNr>"  
                    "<Zip>01123</Zip>"  
                "</Address>"  
            "</Employee>"  
        "</Employees>";

}

And this does not work. I know is like text and can't work. Another special has is don't work too.

enter image description here

Is there another way to paste an XML record with fields in "<>" in HTML?

I don't know how I can do this without adding another variable to any single record. Someone can help me?

CodePudding user response:

There are several ways to solve this but the best would be to define the html structor in the html part of the razor file and reference to the variables instead of define it as a string. To avaoid your '<' are rendered you need to escape them by &lt;?. Same for other Special caracters. This is why your solution not work, the whole string is not rended by the html engine, include the <br>. But that is good because otherwise also <Address> and other xml-tags would be rendered.

<div>
&lt;?xml version=&#39;1.0&#39;?&gt;&quot; 
    &quot;&lt;Employee&gt;&quot; 
        &quot;&lt;Employee&gt;&quot; 
            &quot;&lt;FirstName&gt;First&lt;/FirstName&gt;&quot; 
            &quot;&lt;LastName&gt;Second&lt;/LastName&gt;&quot; 
            &quot;&lt;ContactNo&gt;1234567890&lt;/ContactNo&gt;&quot; 
            &quot;&lt;Email&gt;[email protected]&lt;/Email&gt;&quot; 
            &quot;&lt;Address&gt;<br>quot; 
                &quot;&lt;City&gt;City&lt;/City&gt;&quot; 
                &quot;&lt;Street&gt;Street&lt;/Street&gt;&quot; 
                &quot;&lt;StreetNr&gt;1&lt;/StreetNr&gt;&quot; 
                &quot;&lt;HouseNr&gt;1&lt;/HouseNr&gt;&quot; 
                &quot;&lt;Zip&gt;01123&lt;/Zip&gt;&quot; 
            &quot;&lt;/Address&gt;&quot; 
        &quot;&lt;/Employee&gt;&quot; 
    &quot;&lt;/Employees&gt;&quot;
</div>

CodePudding user response:

I'm not pretty sure I think Code will work fine like this..

<Employee><br/>  
            <Employee><br/>  
                <FirstName>"First"</FirstName><br/>  
  • Related