locked
Create XML file with custom formatting RRS feed

  • Question

  • User769474054 posted

    I need to create an XML file with line breaks and tabs in Attributes and on few tags as well. So I tried like below.

    string xmlID = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<PersonLib Version=\"1.0\"></PersonLib>";
    XDocument doc = XDocument.Parse(xmlID);//, LoadOptions.PreserveWhitespace);
    XElement device = doc.Root;
    using (StringWriter str = new StringWriter())
    using (XmlTextWriter xml = new XmlTextWriter(str))
    {
        xml.Formatting=Formatting.Indented;
    
        xml.WriteStartElement("Details");
        xml.WriteWhitespace("\n\t");
        xml.WriteStartElement("Name");
        xml.WriteWhitespace("\n\t\t");
        xml.WriteStartElement("JohnDoe");
        xml.WriteAttributeString("DOB", "10");
        xml.WriteAttributeString("FirstName", "20");
        xml.WriteAttributeString("LastName", "40");
        xml.WriteAttributeString("\n\t\t\tAddress", "50");
        xml.WriteAttributeString("\n\t\t\tPhoneNum", "60");
        xml.WriteAttributeString("\n\t\t\tCity", "70");
        xml.WriteAttributeString("\n\t\t\tState", "80");
        xml.WriteAttributeString("\n\t\t\tCountry", "90");
        //xml.WriteWhitespace("\n\t\t");
        xml.WriteEndElement();
        xml.WriteWhitespace("\n\t");
        xml.WriteEndElement();
        xml.WriteWhitespace("\n");
        xml.WriteEndElement();
        Console.WriteLine(str);
    
        device.Add(XElement.Parse(str.ToString(), LoadOptions.PreserveWhitespace));
    
        File.WriteAllText("MyXML.xml", device.ToString());

    I can get the XML generated in format I need but the issue comes when I try to add it to the parent XMLElement device in this case. The formatting is all gone despite LoadOptions.PreserveWhitespace.

    I get

    <PersonLib Version="1.0">
      <Details>
        <Name>
            <JohnDoe DOB="10" FirstName="20" LastName="40" Address="50" PhoneNum="60" City="70" State="80" Country"90" />
        </Name>
    </Details>
    </PersonLib >

    while I need

    <PersonLib Version="1.0">
        <Details>
            <Name>
                <JohnDoe DOB="10" FirstName="20" LastName="40"
                            Address="50"
                            PhoneNum="60"
                            City="70"
                            State="80"
                            Country="90" />
            </Name>
        </Details>
    </PersonLib >

    Not sure what am I missing.

    Wednesday, March 28, 2018 5:03 AM

All replies

  • User753101303 posted

    Hi,

    My guess is that a valid attribute name is expected and that those characters are just ignored. Try maybe WriteRaw instead :

     xml.WriteRaw("\n\t\t");
     xml.WriteAttributeString("PhoneNum", "60");
    

    Also make sure to not overdesign:
    - those who would need to see the XML content should have an editor that can reformat this as needed (such as VS)
    - if the other side requires this for processing the file, it is badly written and having this to be fixed would be likely better

    Wednesday, March 28, 2018 7:13 AM