C# Get all text from xml node including xml markup using Visual studio generated class

后端 未结 1 1906
灰色年华
灰色年华 2021-01-25 04:29

Using xml to c# feature in visual studio converted the below xml markup into C# class.




Auth         


        
相关标签:
1条回答
  • 2021-01-25 05:21

    You can use [XmlAnyElement("name")] to capture the XML of the <name> node of each author into an XmlElement (or XElement). The precise text you want is the InnerXml of that element:

    [XmlRoot(ElementName = "author")]
    public class Author
    {
        [XmlAnyElement("name")]
        public XmlElement NameElement { get; set; }
    
        [XmlIgnore]
        public string Name
        {
            get
            {
                return NameElement == null ? null : NameElement.InnerXml;
            }
            set
            {
                if (value == null)
                    NameElement = null;
                else
                {
                    var element = new XmlDocument().CreateElement("name");
                    element.InnerXml = value;
                    NameElement = element;
                }
            }
        }
    }
    

    This eliminates the need for the booksBookAuthorName class.

    Sample fiddle showing deserialization of a complete set of classes corresponding to your XML, generated initially from http://xmltocsharp.azurewebsites.net/ after which Author was modified as necessary.

    0 讨论(0)
提交回复
热议问题