Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
C# 序列化时如何添加实体?_C#_Xml_Xsd - Fatal编程技术网

C# 序列化时如何添加实体?

C# 序列化时如何添加实体?,c#,xml,xsd,C#,Xml,Xsd,问题 我是否可以修改POCO类,以便在序列化时包含所需的实体s?阅读之后,我意识到我的XML应该包含下面的DOCTYPE,我只是不知道如何插入它 <?xml version="1.0" encoding="utf-16"?> <!DOCTYPE documentElement[<!ENTITY NZ "NZ"><!ENTITY AU "AU">]> <ValuationTransaction xmlns:xsd="http://www.w3.

问题

我是否可以修改POCO类,以便在序列化时包含所需的
实体
s?阅读之后,我意识到我的XML应该包含下面的
DOCTYPE
,我只是不知道如何插入它

<?xml version="1.0" encoding="utf-16"?>
<!DOCTYPE documentElement[<!ENTITY NZ "NZ"><!ENTITY AU "AU">]>
<ValuationTransaction xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://vx.valex.com.au/lixi/schema/1.3.5/ValuationTransaction.xsd" ProductionData="Yes">
    ... etc.
</ValuationTransaction>
背景

我正在使用
XmlSerializer
StringWriter
将根类序列化为字符串,然后使用来自的xmlvalidater验证XML字符串。验证XML时,验证器会说

对未声明实体“NZ”的引用。第37行第24位

。。。它引用此元素的
ISO3166
属性

<Country ISO3166="NZ">NEW ZEALAND</Country>
值的范围仅限于
NZ
AU
。我可以像这样添加
DOCTYPE
,但显然我宁愿找到更好的方法:

var entitiesDtd = @"<!DOCTYPE documentElement[<!ENTITY NZ ""NZ""><!ENTITY AU ""AU"">]>" + Environment.NewLine;
xmlString = xmlString.Insert(xmlString.IndexOf("<ValuationTransaction"), entitiesDtd);

我稍微改变了我的序列化方式,因此它包含了一个
XmlWriter

XmlSerializer xsSubmit=新的XmlSerializer(typeof(ValuationTransaction));
var subReq=payloadPoco;
var xml=“”;
使用(var sww=new StringWriter())
{
var writerSettings=新的XmlWriterSettings();
writerSettings.Indent=true;
使用(XmlWriter=XmlWriter.Create(sww,writerSettings))
{
WriteDocType(“documentElement”,null,null,”);
xsSubmit.Serialize(writer,subReq);
xml=sww.ToString();//您的xml
返回xml;
}
}

您需要添加设置

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(sww, settings);

谢谢-我只是在你写的时候发现的。但不确定这本身是否可以作为问题的答案。您缺少一个名称空间。在xml中应该有xmlns:NZ。验证程序似乎很高兴在实体上没有名称空间,而上的示例没有名称空间。您能说明为什么需要名称空间吗?如果删除:[System.Xml.Serialization.XmlAttributeAttribute(DataType=“ENTITY”)]是否有效
var entitiesDtd = @"<!DOCTYPE documentElement[<!ENTITY NZ ""NZ""><!ENTITY AU ""AU"">]>" + Environment.NewLine;
xmlString = xmlString.Insert(xmlString.IndexOf("<ValuationTransaction"), entitiesDtd);
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ValuationTransaction
{
    [XmlAttribute("noNamespaceSchemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string noNamespaceSchemaLocation = "https://vx.valex.com.au/lixi/schema/1.3.5/ValuationTransaction.xsd";
    ... etc.
}
XmlSerializer xsSubmit = new XmlSerializer(typeof(ValuationTransaction));
var subReq = payloadPoco;
var xml = "";

using (var sww = new StringWriter())
{
    var writerSettings = new XmlWriterSettings();
    writerSettings.Indent = true;

    using (XmlWriter writer = XmlWriter.Create(sww, writerSettings))
    {
        writer.WriteDocType("documentElement", null, null, "<!ENTITY AU \"Australia\"><!ENTITY NZ \"New Zealand\">");
        xsSubmit.Serialize(writer, subReq);
        xml = sww.ToString(); // Your XML
        return xml;
    }
}
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(sww, settings);