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" IsDo开发者_Go百科mainGroup="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?
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);
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?
This is my final solution:
result = XDocument.Parse(e.Result.ToString());
XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
XNamespace ns = result.Root.GetDefaultNamespace();
nsManager.AddNamespace("def", ns.NamespaceName);
IEnumerable<XElement> users = result.XPathSelectElements("/def:GetAllUserCollectionFromWeb/def:Users/def:User", nsManager);
foreach (XElement u in users)
{
persons.Add(new Person()
{
ID = u.Attribute("ID").Value,
LoginName = u.Attribute("LoginName").Value
});
}
Thanks for helping!
精彩评论