简体   繁体   English

XMLSerializer-是否将类添加到命名空间?

[英]XMLSerializer - Class added to Namespace?

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 . 我的Listbox会根据组合Combobox选择的项目将生成的项目添加到其中。 When I click the button Create a new Window appears with a WebBrowser object inside of it. 单击“ Create新窗口”按钮时,将出现一个WebBrowser对象。

(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 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). 然后,它使用名称为Standards的项目列表(列表框中的项目)来创建Alrighty类的新实例。 The property gets populated with the third line and then I have another class named SaveXML (See Below) and this saves in XML. 该属性用第三行填充,然后我有另一个名为SaveXML的类(请参见下文),该类保存为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 SaveXML类别

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: 当我单击按钮并使用Web Browser进入窗口时,出现以下代码:

http://imgur.com/zF465n5 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. 从蓝色框中可以看到,当我删除此代码并为我的Stylesheet添加代码时,一切正常,但是问题是蓝色框中的代码不断生成,是否有一种方法可以不获取此代码创建的XML文件。

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); }

}

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. 如我在评论中所述,要添加定制属性,请实现IXmlSerializable并实现WriteXml并添加您的定制属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM