简体   繁体   English

将包含 xml 代码的字符串转换为 XmlTextWriter 的问题

[英]problems with converting string which contains xml code into XmlTextWriter

I have a string variable Writer that recieves xml code from another window .我有一个string变量Writer ,它从另一个window接收 xml 代码。 I need to pass this string to textWriter variable but i dont know how to make it readable by textWriter .我需要将此string传递给textWriter变量,但我不知道如何使它可以被textWriter读取。 I tried to convert it to XmlTextWriter but it dont seem to work.我试图将它转换为XmlTextWriter但它似乎不起作用。 How can it be accepted by textWriter ?它如何被textWriter接受?

textWriter = (XmlTextWriter)XmlTextWriter.Create(new StringBuilder(Writer));
XmlTextWriter textWriter; 

public Question_Redactor(int k, string Writer)
{          
    textWriter =  new XmlTextWriter(Writer);
    InitializeComponent();
}

When the code runs, the following error message is generated:代码运行时,会生成以下错误消息:

The system.InvalidCastException: "Unable to cast objects of type" System. system.InvalidCastException:“无法转换类型的对象”系统。 Xml. Xml。 XmlWellFormedWriter "to type" System. XmlWellFormedWriter "to type" 系统。 Xml. Xml。 XmlTextWriter"." XmlTextWriter"。"

Did like jdweng suggested and it worked perfectlyjdweng建议的那样,它工作得很好

XmlWriter testWriter; 

        public Question_Redactor(int k, string Writer)
        {
            StringBuilder builder = new StringBuilder(Writer);
            XmlWriter writer = XmlWriter.Create(builder);
            testWriter = writer;

            InitializeComponent();

          
        }

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

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