Question

I have been stuck on this problem for the last 2 days and still haven't made it work and I am looking for some help.

My Listbox gets generated items added to it depending on the item selected in the Combobox. When I click the button Create a new Window appears with a WebBrowser object inside of it.

(Wasnt allowed to upload image so it is contained in the link)

This is the screen with the list box with the items generated and also the button that is clicked.

http://imgur.com/6B8GO1m

Button Click Event

This gets the item selected in the Combobox. Then it creates a new instance of the Alrighty Class with the property called Standards with the List of items (The items from the Listbox). The property gets populated with the third line and then I have another class named SaveXML (See Below) and this saves in XML. Then will open the browser.

string selectedStandard = (string)cmbStandard.SelectedItem;
Alrighty info = new Alrighty();
info.Standards = _standardDefinitions;
SaveXML.SaveData(info, string.Format("{0}.xml", selectedStandard));

HTMLBrowser boss = new HTMLBrowser(selectedStandard);
boss.Show();

SaveXML Class

public static void SaveData(object obj, string filename)
{
    XmlSerializer sr = new XmlSerializer(obj.GetType());
    TextWriter writer = new StreamWriter(filename);
    sr.Serialize(writer, obj);
    writer.Close();
}

The Problem

When I click the button and get onto the Window with the Web Browser on this code appears:

http://imgur.com/zF465n5

As you can see from the blue box, When I delete this code and add the code in for my Stylesheet everything works fine, but the problem is the code in the blue box keeps getting generated, is there a way to not get this code in the XML file that is created.

Extra

How can I get this string to appear instead of the generated code in the blue box:

<?xml-stylesheet type="text/xsl" href="StandardXS.xsl"?>

EDIT:

public class SaveXML
{
    public static void SaveData(object obj, string filename)
    {

        //empty namespace and empty value
        XmlSerializerNamespaces alright = new XmlSerializerNamespaces();

        alright.Add("", "");

        XmlSerializer sr = new XmlSerializer(obj.GetType());
        TextWriter writer = new StreamWriter(filename);

        sr.Serialize(writer, obj, alright);
        writer.Close();

    }

    public void WriteXml(XmlWriter writer) { writer.WriteAttributeString(@"<?xml-stylesheet type=text/xsl href=StandardXS.xsl?>", string.Empty); }

}
Was it helpful?

Solution

You can do it like this:

XmlSerializerNamespaces namespace = new     XmlSerializerNamespaces();

//empty namespace and empty value
namespace.Add("", "");

XmlSerializer serializer = new XmlSerializer(someType);

//Serialize the object with custom namespace
serializer.Serialize(xmlTextWriter, myObj, namespace);

For adding a custom attribute, as I said in the comments, implement IXmlSerializable and implement WriteXml and add your custom attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top