0

I have build some xml tag using Stringbuilder. This Stringbuilder string I want to convert as string in XML format using C#. Below I have specified input string and expected output string.

EX:

Input:
      <Configuration Id="5020244c-42c4-4a3c-af16-806d8948c7fd" Name="ACTDrawing" Landscape="True" PaperKind="A3" IsNewConfiguration="true"><Worksheets><WorkSheet Name="OVERVIEW" Zoom="100" Lock="False" Default="True"> <Page Id="7eba0912-51bf-4ffe-ac65-6ac495fa5af6" Number="1" Lock="False"></WorkSheet></Worksheets></Configuration>

Expected Output:
      <Configuration Id=\"5020244c-42c4-4a3c-af16-806d8948c7fd\" Name=\"ACTDrawing\" Landscape=\"True\" PaperKind=\"A3\" IsNewConfiguration=\"true\"><Worksheets><WorkSheet Name=\"OVERVIEW\" Zoom=\"100\" Lock=\"False\" Default=\"True\"><Page Id=\"7eba0912-51bf-4ffe-ac65-6ac495fa5af6\" Number=\"1\" Lock=\"False\"></WorkSheet></Worksheets></Configuration>

I have done code for this conversion.

Ex :

    StringBuilder ACTConfigXML = new StringBuilder();
ACTConfigXML.Append("<Configuration Id=\"");
ACTConfigXML.Append(System.Guid.NewGuid());
ACTConfigXML.Append("\" Name=\"ACTDrawing\" " +
    "Landscape=\"True\" PaperKind=\"A3\" " +
    "IsNewConfiguration=\"true\">");
ACTConfigXML.Append("<Worksheets>");
ACTConfigXML.Append("<WorkSheet Name=\"");
ACTConfigXML.Append(_WorkSheetName.ToString());
ACTConfigXML.Append("\" Zoom=\"100\" Lock=\"False\" Default=\"False\">");
ACTConfigXML.Append("</WorkSheet>");
ACTConfigXML.Append("</Worksheets></Configuration>");

XmlDocument _ACTGraphicalXMLDoc = new XmlDocument();
_ACTGraphicalXMLDoc.LoadXml(ACTConfigXML.ToString());
ACTConfigXML = new StringBuilder();
ACTConfigXML.Append(_ACTGraphicalXMLDoc.OuterXml);
string configXML = ACTConfigXML.ToString();

In this example I don't want to use "_ACTGraphicalXMLDoc.OuterXml" for formatting. I want to convert "ACTConfigXML" data to Expected format.

4 Answers 4

0

If that is your input, then just save it as an XML file. You don't need the escaped double quotes in XML (you only need that if you are putting in a literal in your program). Your question doesn't explain what you have tried, etc. nor does it address the end goal.

0

From what you've posted, it looks like string.replace() might do the trick

output = input.replace("{", "\"");
output = output.replace("\"", "\\\"");
0

You could save your string with XmlDocument

0

It's rarely a good idea to build a document from strings. You're setting yourself up for XML errors if the "_WorkSheetName.ToString()" contains anything that will break XML (like a "/>...) Create a template XML document in a string or disk file, load into DOM with XmlDomDocument.loadXML, update the values / attributes you need. For example

string strXML = @"
<Configuration id='to be replaced' Landscape='True' PaperKind='A3' IsNewConfiguration='true'>
    <Worksheets>
        <Worksheet Name='to be replaced' Zoom='100' Lock='False' Default='False'/>
    </Worksheets>
</Configuration>";

XmlDocument xml = new XmlDocument();
xml.LoadXml( strXML );
XmlElement ndElement = xml.DocumentElement;
ndElement.SetAttribute( "id", System.Guid.NewGuid().ToString() );

XmlElement ndWorksheet = (XmlElement)xml.SelectSingleNode( "/Configuration/Worksheets/Worksheet" );
if( ndWorksheet != null ) {
    ndWorksheet.SetAttribute("Name", "Safe worksheet name <>'\"/>");
}

xml.Save( Response.OutputStream );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.