C#XML反序列化。从节点读取所有内部文本到字符串属性 [英] C# XML Deserialization. Read all inner text from node into string property

查看:363
本文介绍了C#XML反序列化。从节点读取所有内部文本到字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试修改类,以使模型上的text属性包含某个节点( text )节点的所有内部文本。

I am currently trying to modify my classes so that a my text property on my model contains all inner text of a certain node (text) node.

给我一​​些问题的xml示例是:

An example of the xml which is giving me issues is:

            <component>
                <section>
                    <title>Reason for Visit</title>
                    <text>
                        <content ID="ID3EZZKACA">No Reason for Visit was given.</content>
                    </text>
                </section>
            </component>

我的目标是模型的 text 属性

< content ID = ID0EAAKACA>未给出访问原因。< / content>

当前我的模型如下:

public partial class ComponentSection {
    //other model properties here
    private string textField;

    [System.Xml.Serialization.XmlTextAttribute()]
  public string text {
        get {
            return this.textField;
        }
        set {
            this.textField = value;
        }
    }
    //getters/setters for other properties here
}

因此,我目前正在尝试通过使用注释 [System.Xml.Serialization.XmlTextAttribute()] 完成此操作,

So, I am currently trying to accomplish this by using the annotation [System.Xml.Serialization.XmlTextAttribute()] however when I do so, the text property is always coming through as null when the xml is deserialized.

推荐答案

正如我在评论中说的那样,以序列化通常更容易。对于上述XML,下面是一些类

As I said in a comment, starting with serialization is often easier. For the XML above, here are some classes

public sealed class component
{
    public section section { get; set; }
}
public sealed class section
{
    public string title { get; set; }
    public text text { get; set; }
}
public sealed class text
{
    public content content { get; set; }
}
public sealed class content
{
    public string text { get; set; }
    public string ID { get; set; }
}

然后,将 content 类修改为< a href = https://msdn.microsoft.com/zh-cn/library/83y7df3e(v=vs.110).aspx rel = nofollow noreferrer>控制XML序列化:

public sealed class content
{
    [XmlText]
    public string text { get; set; }
    [XmlAttribute]
    public string ID { get; set; }
}

然后可以使用以下代码序列化实例:

You can then serialize an instance with the following code:

        static string ToXmlString<T>(T t)
        {
            var serializer = new XmlSerializer(t.GetType());
            using (var sw = new System.IO.StringWriter())
            {
                serializer.Serialize(sw, t);
                return sw.ToString();
            }
        }
        static void Main(string[] args)
        {
            var c = new component { section = new section {
                            title = "Reason for Visit", text = new text { content = new content {
                            ID = "ID3EZZKACA", text = "No Reason for Visit was given." } } } };

            string s = ToXmlString(c);
            Console.WriteLine(s);
        }

结果为以下XML:

<?xml version="1.0" encoding="utf-16"?>
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http
://www.w3.org/2001/XMLSchema">
  <section>
    <title>Reason for Visit</title>
    <text>
      <content ID="ID3EZZKACA">No Reason for Visit was given.</content>
    </text>
  </section>
</component>

这篇关于C#XML反序列化。从节点读取所有内部文本到字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆