如何将复杂类型与 xs:any / ##any 一起使用并混合在 XSD 工具生成的代码中

发布于 2024-10-17 06:11:33 字数 1001 浏览 3 评论 0原文

我的 XML 模式中有以下复杂类型:

<xs:complexType name="Widget" mixed="true">
    <xs:sequence>
        <xs:any namespace="##any" processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

派生 XML 中的元素可以包含字符串或可以包含格式良好的 XML,因此混合属性为 true。

当我通过 .NET XSD 工具运行它时,我得到以下生成代码:

public partial class Widget{

    private System.Xml.XmlNode[] anyField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    [System.Xml.Serialization.XmlAnyElementAttribute()]
    public System.Xml.XmlNode[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
        }
    }
}

我的问题是我不完全确定应该如何使用它。最终我需要能够将小部件的值设置为:

<widget>Hello World!</widget>

<widget>
  <foo>Hello World</foo>
</widget>

两者都根据架构进行验证

I have the following complex type in my XML schema:

<xs:complexType name="Widget" mixed="true">
    <xs:sequence>
        <xs:any namespace="##any" processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

The element in derived XML could contain string or could contained wellformed XML, hence the mixed attribute being true.

When I run this through the .NET XSD Tool I get the following generate code:

public partial class Widget{

    private System.Xml.XmlNode[] anyField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    [System.Xml.Serialization.XmlAnyElementAttribute()]
    public System.Xml.XmlNode[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
        }
    }
}

The question I have is that I am not entirely sure how I should then use this. Ultimately I need to be able to set the value of widget to either:

<widget>Hello World!</widget>

or

<widget>
  <foo>Hello World</foo>
</widget>

Both of which validate agaisnt the schema

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-10-24 06:11:33

用于此:

<widget>  
    <foo>Hello World</foo>
</widget>

使用此:

XmlDocument dom = new XmlDocument();
Widget xmlWidget = new Widget();
xmlWidget.Any = new XmlNode[1];
xmlWidget.Any[0] = dom.CreateNode(XmlNodeType.Element, "foo", dom.NamespaceURI);
xmlWidget.Any[0].InnerText = "Hello World!";

用于此:

<widget>Hello World!</widget>

使用此:

XmlDocument dom = new XmlDocument();
XmlNode node = dom.CreateNode(XmlNodeType.Element, "foo", dom.NamespaceURI);
node.InnerText = "Hello World";

Widget w = new Widget();
w.Any = new XmlNode[1];
w.Any[0] = node.FirstChild; 

For this:

<widget>  
    <foo>Hello World</foo>
</widget>

Use this:

XmlDocument dom = new XmlDocument();
Widget xmlWidget = new Widget();
xmlWidget.Any = new XmlNode[1];
xmlWidget.Any[0] = dom.CreateNode(XmlNodeType.Element, "foo", dom.NamespaceURI);
xmlWidget.Any[0].InnerText = "Hello World!";

For this:

<widget>Hello World!</widget>

Use this:

XmlDocument dom = new XmlDocument();
XmlNode node = dom.CreateNode(XmlNodeType.Element, "foo", dom.NamespaceURI);
node.InnerText = "Hello World";

Widget w = new Widget();
w.Any = new XmlNode[1];
w.Any[0] = node.FirstChild; 
秋意浓 2024-10-24 06:11:33

将此作为答案发布,因为从技术上讲它有效并回答了问题。然而,这似乎是一个非常令人讨厌的黑客行为。因此,如果有人有替代和更好的解决方案,我会洗耳恭听。

string mystring= "if I check this code in it will at least have comedy value";

XmlDocument thisLooksBad = new XmlDocument();
thisLooksBad.LoadXml("<temp>" + mystring + "</temp>");

Widget stringWidget = new Widget();
stringWidget.Any = new XmlNode[1];
stringWidget.Any[0] = thisLooksBad.SelectSingleNode("/temp").FirstChild;

正如您所看到的,我将字符串放入标记中的 XmlDocument 中,这可以正常工作、编译和序列化,没有任何问题 - 所以,是的,这是一个解决方案,但让我感到这是一个令人讨厌的黑客攻击。

string myxml = "<x><y>something</y></x>";

XmlDocument thisDoesntLookSoBad = new XmlDocument();
thisLooksBad.LoadXml(myxml);

Widget xmlWidget = new Widget();
xmlWidget.Any = new XmlNode[1];
xmlWidget.Any[0] = thisDoesntLookSoBad;

在此示例中,我将 XML 放入 XmlDocument 中,然后将其分配给生成的类。这更有意义,因为我使用的是 XML 而不是原始字符串。

奇怪的是我也可以这样做并且它也有效(但也是一个令人讨厌的黑客):

string myxml = "<x><y>something</y></x>";

XmlDocument dom = new XmlDocument();
dom.LoadXml("<temp>" + myxml + "</temp>");

Widget xmlWidget = new Widget();
xmlWidget.Any = new XmlNode[1];
xmlWidget.Any[0] = dom.SelectSingleNode("/temp").FirstChild;

Posting this as a answer as technically it works and answers the question. However it seems like a really nasty hack. So if anybody has an alternative and better solution I am all ears.

string mystring= "if I check this code in it will at least have comedy value";

XmlDocument thisLooksBad = new XmlDocument();
thisLooksBad.LoadXml("<temp>" + mystring + "</temp>");

Widget stringWidget = new Widget();
stringWidget.Any = new XmlNode[1];
stringWidget.Any[0] = thisLooksBad.SelectSingleNode("/temp").FirstChild;

As you can see I am placing my string into an XmlDocument wrapped in tags, this works, compiles and serializes without issues - so yes it is a solution but it strikes me that this is a nasty hack.

string myxml = "<x><y>something</y></x>";

XmlDocument thisDoesntLookSoBad = new XmlDocument();
thisLooksBad.LoadXml(myxml);

Widget xmlWidget = new Widget();
xmlWidget.Any = new XmlNode[1];
xmlWidget.Any[0] = thisDoesntLookSoBad;

In this example I am placing my XML into an XmlDocument and then assigning that to the generated class. This makes more sense as I am working with XML not raw string.

Curiously I can also do this and it works as well (but is also a nasty hack):

string myxml = "<x><y>something</y></x>";

XmlDocument dom = new XmlDocument();
dom.LoadXml("<temp>" + myxml + "</temp>");

Widget xmlWidget = new Widget();
xmlWidget.Any = new XmlNode[1];
xmlWidget.Any[0] = dom.SelectSingleNode("/temp").FirstChild;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文