简体   繁体   English

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

[英]How can I use CDATA with generated C# classes from xsd.exe?

Problem 问题

I'm trying to fill in a section at the moment that maps to an xs:string and write this to XML. 我正在尝试填写一个映射到xs:string并将其写入XML。

I've put the generated code of this value at the bottom of this post as it's a bit long. 我将这个值的生成代码放在这篇文章的底部,因为它有点长。

Previously I just assigned it the string value. 以前我只是给它分配了字符串值。

rawdata.data = generatedString;

But when I tried this. 但是当我尝试这个。

rawdata.data = "<![CDATA[" + generatedString + "]]>";

The eventual output formats the CDATA part anyway. 最终输出仍然会格式化CDATA部分。

&lt;![CDATA[

Is there any way that I can avoid this happening so that CDATA appears as it's meant to? 有什么办法可以避免这种情况的发生,以使CDATA像预期的那样出现?

Extra Information 额外的信息

Generated code for this field. 为该字段生成的代码。

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

The answer from @SeM is what I think is the most correct solution according to how Microsoft built the XML serializer but as I need to regenerate the classes from XSD relatively often I decided it was better to try to find another solution instead of manually editing the generated classes after every time they're built. 根据微软如何构建XML序列化程序,@ SeM的答案是我认为是最正确的解决方案,但是由于我需要相对频繁地从XSD重新生成类,因此我决定最好寻找另一个解决方案,而不是手动编辑该解决方案。每次构建后都会生成类。

In this vein I found that instead of modifying the generated classes, I could override the XmlSerializer so that if it came across CDATA content it would be able to handle it. 本着这种精神,我发现不必修改生成的类,而可以覆盖XmlSerializer,这样,如果它遇到CDATA内容,便可以处理它。

Of course this only works if the CDATA is at the very start and very end of the element. 当然,这仅在CDATA位于元素的开头和结尾时才有效。 It suits my use case in that regard but does not universally achieve all use cases. 在这方面,它适合我的用例,但不能普遍实现所有用例。

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

And the custom writer. 和风俗作家。

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

}

It looks like this is along the lines of which Microsoft took. 看起来这是Microsoft采取的方针。

https://referencesource.microsoft.com/#SMDiagnostics/System/ServiceModel/Diagnostics/PlainXmlWriter.cs,137 https://referencesource.microsoft.com/#SMDiagnostics/System/ServiceModel/Diagnostics/PlainXmlWriter.cs,137

Use XmlCDataSection for that: 为此 ,请使用XmlCDataSection

[XmlElement("data")]
public System.Xml.XmlCDataSection Data { get; set; }

it will automatically create CData sections in your xml, if you serialize your object. 如果序列化对象,它将在xml中自动创建CData节。

Assign it: 分配它:

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

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

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