解析 Facebook 帖子 XML

huangapple go评论60阅读模式
英文:

Parse Facebook Post XML

问题

The issue you are encountering with the XML deserialization seems to be related to the XML namespace specified in your XML document. In your code, you have specified the root element's name as "socialMediaMessage," but it appears that the actual XML has a namespace associated with it. To resolve this issue, you should define the XML namespace in your C# classes.

Here's an updated version of your C# classes with the XML namespace included:

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

[Serializable, XmlRoot("socialMediaMessage", 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
{
    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 fromProfile 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; }
}

public class pageProfile
{
    public string userId { get; set; }
    public string name { get; set; }
    public string email { 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 name { get; set; }
    public string email { get; set; }
    public string urlLink { get; set; }
    public string category { get; set; }
    public string nLikes { get; set; }
}

By adding the Namespace attribute to the XmlRoot attribute on the socialMediaMessage class, you should be able to deserialize the XML without encountering the "was not expected" error.

英文:

I need to parse XML sent y facebook after message is posted on web page. XML is as below :

<!-- begin snippet: js hide: false console: true babel: false -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;socialMediaMessage xmlns=&quot;http://www.test.com/SocialMediaLink&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
&lt;socialMediaSource&gt;Facebook&lt;/socialMediaSource&gt;
&lt;facebookMessage&gt;
&lt;protocolVersion&gt;1&lt;/protocolVersion&gt;
&lt;direction&gt;Inbound&lt;/direction&gt;
&lt;inbound&gt;
&lt;action&gt;Event_NewComment&lt;/action&gt;
&lt;event_NewComment&gt;
&lt;uCIAccountName&gt;altitude456@gmail.com&lt;/uCIAccountName&gt;
&lt;pageProfile&gt;
&lt;userId&gt;853738988027292&lt;/userId&gt;
&lt;name&gt;Altitudetele&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;https://www.facebook.com/853738988027292&lt;/urlLink&gt;
&lt;category&gt;Telecommunication company&lt;/category&gt;
&lt;nLikes&gt;5&lt;/nLikes&gt;
&lt;/pageProfile&gt;
&lt;newComment&gt;
&lt;postId&gt;853738988027292_162508306545581&lt;/postId&gt;
&lt;commentId&gt;162508306545581_673058007960935&lt;/commentId&gt;
&lt;parentCommentId&gt;&lt;/parentCommentId&gt;
&lt;fromProfile&gt;
&lt;userId&gt;6084755568286980&lt;/userId&gt;
&lt;name&gt;Dnyati Ganesan&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;&lt;/urlLink&gt;
&lt;category&gt;&lt;/category&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;/fromProfile&gt;
&lt;message&gt;hello&lt;/message&gt;
&lt;urlLink&gt;https://www.facebook.com/159233336873078/posts/162508306545581&lt;/urlLink&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;createdTime&gt;11/05/2023 02:59:28&lt;/createdTime&gt;
&lt;/newComment&gt;
&lt;historical_post&gt;
&lt;postId&gt;853738988027292_162508306545581&lt;/postId&gt;
&lt;fromProfile&gt;
&lt;userId&gt;853738988027292&lt;/userId&gt;
&lt;name&gt;Altitudetele&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;https://www.facebook.com/853738988027292&lt;/urlLink&gt;
&lt;category&gt;Telecommunication company&lt;/category&gt;
&lt;nLikes&gt;5&lt;/nLikes&gt;
&lt;/fromProfile&gt;
&lt;message&gt;test&lt;/message&gt;
&lt;urlLink&gt;https://www.facebook.com/159233336873078/posts/162508306545581&lt;/urlLink&gt;
&lt;attachmentName&gt;&lt;/attachmentName&gt;
&lt;attachmentCaption&gt;&lt;/attachmentCaption&gt;
&lt;attachmentLink&gt;&lt;/attachmentLink&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;createdTime&gt;15/02/2023 16:32:20&lt;/createdTime&gt;
&lt;updatedTime&gt;11/05/2023 02:59:28&lt;/updatedTime&gt;
&lt;/historical_post&gt;
&lt;historical_listComment&gt;
&lt;comment&gt;
&lt;postId&gt;853738988027292_162508306545581&lt;/postId&gt;
&lt;commentId&gt;162508306545581_5945923735533933&lt;/commentId&gt;
&lt;parentCommentId&gt;&lt;/parentCommentId&gt;
&lt;fromProfile&gt;
&lt;userId&gt;5655483951137611&lt;/userId&gt;
&lt;name&gt;Prem Preet&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;&lt;/urlLink&gt;
&lt;category&gt;&lt;/category&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;/fromProfile&gt;
&lt;message&gt;Hi&lt;/message&gt;
&lt;urlLink&gt;https://www.facebook.com/159233336873078/posts/162508306545581&lt;/urlLink&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;createdTime&gt;05/05/2023 11:51:33&lt;/createdTime&gt;
&lt;/comment&gt;
&lt;comment&gt;
&lt;postId&gt;853738988027292_162508306545581&lt;/postId&gt;
&lt;commentId&gt;162508306545581_1319403308608990&lt;/commentId&gt;
&lt;parentCommentId&gt;&lt;/parentCommentId&gt;
&lt;fromProfile&gt;
&lt;userId&gt;5838927156234053&lt;/userId&gt;
&lt;name&gt;Saurabh Roy&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;&lt;/urlLink&gt;
&lt;category&gt;&lt;/category&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;/fromProfile&gt;
&lt;message&gt;hi&lt;/message&gt;
&lt;urlLink&gt;https://www.facebook.com/159233336873078/posts/162508306545581&lt;/urlLink&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;createdTime&gt;05/05/2023 12:22:04&lt;/createdTime&gt;
&lt;/comment&gt;
&lt;comment&gt;
&lt;postId&gt;853738988027292_162508306545581&lt;/postId&gt;
&lt;commentId&gt;162508306545581_1418183775615277&lt;/commentId&gt;
&lt;parentCommentId&gt;&lt;/parentCommentId&gt;
&lt;fromProfile&gt;
&lt;userId&gt;5838927156234053&lt;/userId&gt;
&lt;name&gt;Saurabh Roy&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;&lt;/urlLink&gt;
&lt;category&gt;&lt;/category&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;/fromProfile&gt;
&lt;message&gt;hi&lt;/message&gt;
&lt;urlLink&gt;https://www.facebook.com/159233336873078/posts/162508306545581&lt;/urlLink&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;createdTime&gt;05/05/2023 12:22:08&lt;/createdTime&gt;
&lt;/comment&gt;
&lt;comment&gt;
&lt;postId&gt;853738988027292_162508306545581&lt;/postId&gt;
&lt;commentId&gt;162508306545581_950267632961220&lt;/commentId&gt;
&lt;parentCommentId&gt;&lt;/parentCommentId&gt;
&lt;fromProfile&gt;
&lt;userId&gt;5838927156234053&lt;/userId&gt;
&lt;name&gt;Saurabh Roy&lt;/name&gt;
&lt;email&gt;&lt;/email&gt;
&lt;urlLink&gt;&lt;/urlLink&gt;
&lt;category&gt;&lt;/category&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;/fromProfile&gt;
&lt;message&gt;hi&lt;/message&gt;
&lt;urlLink&gt;https://www.facebook.com/159233336873078/posts/162508306545581&lt;/urlLink&gt;
&lt;nLikes&gt;0&lt;/nLikes&gt;
&lt;createdTime&gt;05/05/2023 12:22:10&lt;/createdTime&gt;
&lt;/comment&gt;
&lt;/historical_listComment&gt;
&lt;/event_NewComment&gt;
&lt;/inbound&gt;
&lt;/facebookMessage&gt;
&lt;/socialMediaMessage&gt;

<!-- end snippet -->

I tried below to deserialize the XML :

 public socialMediaMessage Deserialize&lt;socialMediaMessage&gt;(string input) where socialMediaMessage : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(socialMediaMessage), new XmlRootAttribute(&quot;socialMediaMessage&quot;));
using (StringReader sr = new StringReader(input))
{
return (socialMediaMessage)ser.Deserialize(sr);
}
}

it is giving me an exception as : System.InvalidOperationException: 'There is an error in XML document (1, 2).'
InvalidOperationException: <socialMediaMessage xmlns='http://www.altitude.com/SocialMediaLink'> was not expected.

my object is :

  [Serializable, XmlRoot(&quot;socialMediaMessage&quot;)]
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 historicalPost { get; set; }
public historical_listComment historicalListComment { get; set; }
}
public class historical_listComment
{
public List&lt;historicalComment&gt; 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 &amp; 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; }
}

where could be the issue?

答案1

得分: 0

以下是您要翻译的内容:

"You were missing the default namespace AND you property names have to match the xml tags exactly including uppercase/lowercase. I made some changes below. You need to fix the rest of the items

<!-- begin snippet: js hide: false console: true babel: false -->

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 = &quot;http://www.test.com/SocialMediaLink&quot;)]
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&lt;historicalComment&gt; 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 &amp; 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; }
}

}

<!-- end snippet -->"

请注意,由于这部分内容是代码,不需要进行翻译。

英文:

You were missing the default namespace AND you property names have to match the xml tags exactly including uppercase/lowercase. I made some changes below. You need to fix the rest of the items

<!-- begin snippet: js hide: false console: true babel: false -->

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 = @&quot;c:\temp\test.xml&quot;;
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(socialMediaMessage));
socialMediaMessage message = (socialMediaMessage)serializer.Deserialize(reader);
}
}
[XmlRoot(Namespace = &quot;http://www.test.com/SocialMediaLink&quot;)]
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&lt;historicalComment&gt; 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 &amp; 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; }
}
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 15:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定