I need to parse XML sent y facebook after message is posted on web page. XML is as below :
<?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>
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: 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?
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
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; }
}
}