Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize custom SOAP/XML to objects

I have this xml/soap from a sharepoint webservice call:

<GetAllUserCollectionFromWeb xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
<Users>
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
    <User ID="ID" Sid="ID" Name="XXX" LoginName="XXX" Email="XXX" Notes="" IsSiteAdmin="False" IsDomainGroup="False" Flags="0" /> 
</Users>

I want to deserialize this to a List<> of the this object:

public class Person
{
    public string ID { get; set; }
    public string Sid { get; set; }
    public string Name { get; set; }
    public string LoginName { get; set; }
    public string Email { get; set; }
    public string Notes { get; set; }
    public string IsSiteAdmin { get; set; }
    public string IsDomainGroup { get; set; }
    public string Flags { get; set; }
}

I tried to use xpath but it doesn't work!

        XDocument result = XDocument.Parse(e.Result.ToString());
        IEnumerable<XElement> ele = result.XPathSelectElements("/def:GetAllUserCollectionFromWeb/def:Users/def:User");

Error:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

What is wrong or how can I solve the problem?

like image 491
Werewolve Avatar asked Jun 14 '26 22:06

Werewolve


2 Answers

Add a namespace manager to your query.

XDocument result = XDocument.Parse(e.Result.ToString());

XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
XNamespace namespace = result.Root.GetDefaultNamespace();
nsManager.AddNamespace("def", namespace.NamespaceName);

IEnumerable<XElement> ele = result.XPathSelectElements("/def:GetAllUserCollectionFromWeb/def:Users/def:User", nsManager);
like image 199
Johann Blais Avatar answered Jun 16 '26 12:06

Johann Blais


You can use the XSD.exe tool to reverse engineer a proxy class from the XML. You may be able to specify the collection type as well, or edit the generated class.

http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.80).aspx

http://sharpertutorials.com/using-xsd-tool-to-generate-classes-from-xml/

Edit: Or just use "add service reference", or SvcUtil.exe to generate a proxy for the whole service?

like image 32
Doobi Avatar answered Jun 16 '26 11:06

Doobi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!