解析 Facebook 帖子 XML

问题描述 投票:0回答:1
xml facebook parsing serialization
1个回答
0
投票

您缺少默认命名空间,并且属性名称必须与 xml 标签完全匹配,包括大写/小写。 我在下面做了一些更改。 您需要修复其余的项目

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication52
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(socialMediaMessage));
            socialMediaMessage message = (socialMediaMessage)serializer.Deserialize(reader);
 
        }
    }
    [XmlRoot(Namespace = "http://www.test.com/SocialMediaLink")]
    public class socialMediaMessage
    {
        public string socialMediaSource { get; set; }
        public facebookMessage facebookMessage { get; set; }
    }
    public class facebookMessage
    {
        public string protocolVersion { get; set; }
        public string direction { get; set; }
        public inbound inbound { get; set; }
    }
    public class inbound
    {
        public string action { get; set; }
        public Event_NewComment event_NewComment { get; set; }
    }
    public class Event_NewComment
    {
        public string uCIAccountName { get; set; }
        public pageProfile PageProfile { get; set; }
        public newComment NewComment { get; set; }
        public historical_post historical_post { get; set; }
        public historical_listComment historical_listComment { get; set; }
    }

    public class historical_listComment
    {
        [XmlElement]
        public List<historicalComment> comment { get; set; }
    }
    public class historicalComment
    {
        public string postId { get; set; }
        public string commentId { get; set; }
        public string parentCommentId { get; set; }
        public pageProfile fromProfile { get; set; }
        public string message { get; set; }
        public string urlLink { get; set; }
        public string nLikes { get; set; }
        public string createdTime { get; set; }
    }
    public class historical_post
    {
        public string postId { get; set; }
        public fromProfile fromProfile { get; set; }
        public string message { get; set; }
        public string urlLink { get; set; }
        public string attachmentName { get; set; }
        public string attachmentCaption { get; set; }
        public string attachmentLink { get; set; }
        public string nLikes { get; set; }
        public string createdTime { get; set; }
        public string updatedTime { get; set; }
    }
    public class newComment
    {
        public string postId { get; set; }
        public string commentId { get; set; }
        public string parentCommentId { get; set; }
        public fromProfile fromProfile { get; set; }
        public string message { get; set; }
        public string urlLink { get; set; }
        public string nLikes { get; set; }
        public string createdTime { get; set; }
    }

    //Page Profile & from profile structure is same
    public class pageProfile
    {
        public string userId { get; set; }
        public string profileName { get; set; }
        public string pageEmail { get; set; }
        public string urlLink { get; set; }
        public string category { get; set; }
        public string nLikes { get; set; }
    }
    public class fromProfile
    {
        public string userId { get; set; }
        public string profileName { get; set; }
        public string pageEmail { get; set; }
        public string urlLink { get; set; }
        public string category { get; set; }
        public string nLikes { get; set; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.