Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将CDATA与xsd.exe生成的C#类一起使用?_C#_Xml_Cdata_Xsd.exe - Fatal编程技术网

如何将CDATA与xsd.exe生成的C#类一起使用?

如何将CDATA与xsd.exe生成的C#类一起使用?,c#,xml,cdata,xsd.exe,C#,Xml,Cdata,Xsd.exe,问题 我正在尝试填充一个映射到xs:string的部分,并将其写入XML 我把这个值的生成代码放在这篇文章的底部,因为它有点长 之前我只是给它赋值 rawdata.data = generatedString; 但是当我尝试这个的时候 rawdata.data = "<![CDATA[" + generatedString + "]]>"; 有没有什么方法可以避免这种情况发生,从而使CDATA看起来像它的本意 额外信息 为此字段生成的代码 /// <remarks/>

问题

我正在尝试填充一个映射到
xs:string
的部分,并将其写入XML

我把这个值的生成代码放在这篇文章的底部,因为它有点长

之前我只是给它赋值

rawdata.data = generatedString;
但是当我尝试这个的时候

rawdata.data = "<![CDATA[" + generatedString + "]]>";
有没有什么方法可以避免这种情况发生,从而使CDATA看起来像它的本意

额外信息

为此字段生成的代码

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DataFilesRawdata
{

    private string idField;

    private string dataField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string data
    {
        get
        {
            return this.dataField;
        }
        set
        {
            this.dataField = value;
        }
    }
}
//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.6.1055.0”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类DataFilesRawdata
{
专用字符串字段;
私有字符串数据字段;
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串ID
{
得到
{
返回此.idField;
}
设置
{
this.idField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串数据
{
得到
{
返回此.dataField;
}
设置
{
this.dataField=值;
}
}
}
使用进行以下操作:

[XmlElement("data")]
public System.Xml.XmlCDataSection Data { get; set; }
如果序列化对象,它将自动在xml中创建CData节

分配给它:

rawdata.data = System.Xml.XmlDocument().CreateCDataSection(generatedString);

根据Microsoft构建XML序列化程序的方式,@SeM给出的答案是我认为最正确的解决方案,但由于我需要相对频繁地从XSD重新生成类,因此我决定最好尝试找到另一个解决方案,而不是在每次生成类后手动编辑生成的类

在这种情况下,我发现我可以重写XmlSerializer,而不是修改生成的类,这样,如果它遇到CDATA内容,它就能够处理它

当然,这只有在CDATA位于元素的最开始和最末尾时才有效。在这方面,它适合我的用例,但并不能普遍实现所有用例

using (var fileStream = new System.IO.FileStream(tempFilePath,FileMode.Create))
{                
    var xmlwriter = new CustomXmlTextWriter(fileStream);
    xmls.Serialize(xmlwriter, contents, ns);
}
还有定制作家

public class CustomXmlTextWriter : XmlTextWriter
{

    //... constructor if you need it

    public override void WriteString(string text)
    {
        if (text.StartsWith("<![CDATA[") && text.EndsWith("]]>"))
        {
            base.WriteRaw(text);
            return;
        }
        base.WriteString(text);
    }

}
公共类CustomXmlTextWriter:XmlTextWriter
{
//…如果你需要的话
公共覆盖无效写入字符串(字符串文本)
{
if(text.StartsWith(“”)
{
base.WriteRaw(文本);
返回;
}
base.WriteString(文本);
}
}
看起来这正是微软采取的方针


尽管需要修改自动生成的类,但我认为这是可行的。我想知道我是否可以利用大多数生成的类都是
partial
类这一事实,在后续的重新生成过程中维护我的更改。它是由什么自动生成的?Microsoft SDK中的XML架构定义工具
xsd.exe
工具。很好的解决方案,真的。
public class CustomXmlTextWriter : XmlTextWriter
{

    //... constructor if you need it

    public override void WriteString(string text)
    {
        if (text.StartsWith("<![CDATA[") && text.EndsWith("]]>"))
        {
            base.WriteRaw(text);
            return;
        }
        base.WriteString(text);
    }

}