简体   繁体   English

是否可以使用属性指定XML节点名称和深度

[英]Is it possible to specify a XML node name and depth with attributes

I would like to serialize a C# class structure out to XML and provide for a specific node name without having to have a bunch of nested classes. 我想将C#类结构序列化为XML并提供特定的节点名称,而不必拥有一堆嵌套类。 Is that possible using attributes? 可以使用属性吗?

For example say I have the following XML: 例如,说我有以下XML:

<OuterItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <InnerItem>
        <ItemValue>something i need</ItemValue>
    </InnerItem>
</OuterItem>

I have an XML serialization method that looks like this: 我有一个XML序列化方法,如下所示:

public static string XmlSerializeToString<T>(T value)
{
    if (value == null) { return null; }

    XmlSerializer serializer = new XmlSerializer(typeof(T));

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = false;
    settings.OmitXmlDeclaration = true;

    using (StringWriter textWriter = new StringWriter())
    using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
    {
        serializer.Serialize(xmlWriter, value);
        return textWriter.ToString();
    }
}

Would I have to have a C# class structure like this? 我必须有这样的C#类结构吗?

public class OuterItem
{
    public InnerItem InnerItem { get; set; }
}

public class InnerItem
{
    public string ItemValue { get; set; }
}

Or is it at all possible to declare how far down in the XML document my node should be with something like this (pseudo code): 或者是否可以通过这样的方式(伪代码)声明我的节点应该在XML文档中有多远:

public class OuterItem
{
    [XmlNode("InnerItem\ItemValue")]
    public string ItemValue { get; set; }
}

No, it is not possible to use xpath or similar expressions in XmlNode attributes. 不,在XmlNode属性中不可能使用xpath或类似表达式。 Your choice of attributes to control xml serialization is very limited. 您选择的控制xml序列化的属性非常有限。

You can use following hack to deserialize xml you have: 您可以使用以下hack来反序列化您拥有的xml:

public class OuterItem
{
    [XmlArrayItem(ElementName="ItemValue", Type=typeof(string))]
    [XmlArray]
    public string[] InnerItem
    {
        get; set;
    }
}

public class Test
{
    static void Main()
    {
        var item = new OuterItem { InnerItem = new[]{"AAA"} };
        XmlSerializer ser = new XmlSerializer(typeof(OuterItem));
        ser.Serialize(Console.Out,item);
    }
}

This would produce following xml: 这会产生以下xml:

<?xml version="1.0" encoding="utf-8"?>
<OuterItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <InnerItem>
    <ItemValue>AAA</ItemValue>
  </InnerItem>
</OuterItem>

Another possible solution is to use XmlTextAttribute, get inner xml into property of XmlNode type, and parse manually. 另一种可能的解决方案是使用XmlTextAttribute,将内部xml转换为XmlNode类型的属性,并手动解析。

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

相关问题 XML节点名称和属性不可用 - XML node name and attributes are not available 如何使用XML属性指定另一个XML名称? - How I can use XML attributes to specify another XML name? 在 nil xml 元素上指定属性 - specify attributes on a nil xml element 类型&#39;X&#39;和X&#39;都使用来自命名空间&#39;&#39;的XML类型名称&#39;X&#39;。 使用XML属性为类型指定唯一的XML名称和/或命名空间 - Types 'X' and X' both use the XML type name, 'X', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type 如何解决“都使用 XML 类型名称 X,使用 XML 属性为类型指定唯一的 XML 名称和/或命名空间”? - How to solve “Both use the XML type name X, use XML attributes to specify a unique XML name and/or namespace for the type”? 向 XML 节点添加属性 - Adding attributes to an XML node 如何动态检索xml节点之一的所有可能的属性(变量属性)值? - How to dynamically retrive the all the possible attributes (variable attributes) values of one of the xml node? Select 和 XML 节点通过 XPath 在任意深度 - Select an XML node via XPath at an arbitrary depth 可以使用Ajax.ActionLink指定表单属性 - Possible to specify form attributes with Ajax.ActionLink 是否可以不指定工作表名称和列? - Is it possible not specify the sheet name and columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM