英文:
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 -->
<?xml version="1.0" encoding="UTF-8"?>
<socialMediaMessage xmlns="http://www.test.com/SocialMediaLink" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<socialMediaSource>Facebook</socialMediaSource>
<facebookMessage>
<protocolVersion>1</protocolVersion>
<direction>Inbound</direction>
<inbound>
<action>Event_NewComment</action>
<event_NewComment>
<uCIAccountName>altitude456@gmail.com</uCIAccountName>
<pageProfile>
<userId>853738988027292</userId>
<name>Altitudetele</name>
<email></email>
<urlLink>https://www.facebook.com/853738988027292</urlLink>
<category>Telecommunication company</category>
<nLikes>5</nLikes>
</pageProfile>
<newComment>
<postId>853738988027292_162508306545581</postId>
<commentId>162508306545581_673058007960935</commentId>
<parentCommentId></parentCommentId>
<fromProfile>
<userId>6084755568286980</userId>
<name>Dnyati Ganesan</name>
<email></email>
<urlLink></urlLink>
<category></category>
<nLikes>0</nLikes>
</fromProfile>
<message>hello</message>
<urlLink>https://www.facebook.com/159233336873078/posts/162508306545581</urlLink>
<nLikes>0</nLikes>
<createdTime>11/05/2023 02:59:28</createdTime>
</newComment>
<historical_post>
<postId>853738988027292_162508306545581</postId>
<fromProfile>
<userId>853738988027292</userId>
<name>Altitudetele</name>
<email></email>
<urlLink>https://www.facebook.com/853738988027292</urlLink>
<category>Telecommunication company</category>
<nLikes>5</nLikes>
</fromProfile>
<message>test</message>
<urlLink>https://www.facebook.com/159233336873078/posts/162508306545581</urlLink>
<attachmentName></attachmentName>
<attachmentCaption></attachmentCaption>
<attachmentLink></attachmentLink>
<nLikes>0</nLikes>
<createdTime>15/02/2023 16:32:20</createdTime>
<updatedTime>11/05/2023 02:59:28</updatedTime>
</historical_post>
<historical_listComment>
<comment>
<postId>853738988027292_162508306545581</postId>
<commentId>162508306545581_5945923735533933</commentId>
<parentCommentId></parentCommentId>
<fromProfile>
<userId>5655483951137611</userId>
<name>Prem Preet</name>
<email></email>
<urlLink></urlLink>
<category></category>
<nLikes>0</nLikes>
</fromProfile>
<message>Hi</message>
<urlLink>https://www.facebook.com/159233336873078/posts/162508306545581</urlLink>
<nLikes>0</nLikes>
<createdTime>05/05/2023 11:51:33</createdTime>
</comment>
<comment>
<postId>853738988027292_162508306545581</postId>
<commentId>162508306545581_1319403308608990</commentId>
<parentCommentId></parentCommentId>
<fromProfile>
<userId>5838927156234053</userId>
<name>Saurabh Roy</name>
<email></email>
<urlLink></urlLink>
<category></category>
<nLikes>0</nLikes>
</fromProfile>
<message>hi</message>
<urlLink>https://www.facebook.com/159233336873078/posts/162508306545581</urlLink>
<nLikes>0</nLikes>
<createdTime>05/05/2023 12:22:04</createdTime>
</comment>
<comment>
<postId>853738988027292_162508306545581</postId>
<commentId>162508306545581_1418183775615277</commentId>
<parentCommentId></parentCommentId>
<fromProfile>
<userId>5838927156234053</userId>
<name>Saurabh Roy</name>
<email></email>
<urlLink></urlLink>
<category></category>
<nLikes>0</nLikes>
</fromProfile>
<message>hi</message>
<urlLink>https://www.facebook.com/159233336873078/posts/162508306545581</urlLink>
<nLikes>0</nLikes>
<createdTime>05/05/2023 12:22:08</createdTime>
</comment>
<comment>
<postId>853738988027292_162508306545581</postId>
<commentId>162508306545581_950267632961220</commentId>
<parentCommentId></parentCommentId>
<fromProfile>
<userId>5838927156234053</userId>
<name>Saurabh Roy</name>
<email></email>
<urlLink></urlLink>
<category></category>
<nLikes>0</nLikes>
</fromProfile>
<message>hi</message>
<urlLink>https://www.facebook.com/159233336873078/posts/162508306545581</urlLink>
<nLikes>0</nLikes>
<createdTime>05/05/2023 12:22:10</createdTime>
</comment>
</historical_listComment>
</event_NewComment>
</inbound>
</facebookMessage>
</socialMediaMessage>
<!-- end snippet -->
I tried below to deserialize the XML :
public socialMediaMessage Deserialize<socialMediaMessage>(string input) where socialMediaMessage : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(socialMediaMessage), new XmlRootAttribute("socialMediaMessage"));
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("socialMediaMessage")]
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<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; }
}
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 = "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; }
}
}
<!-- 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 = @"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; }
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论