我已经看过这里的所有答案,我已经尝试了所有的方法,但是XmlSerializer只是部分地将XML序列化成一个C#类。
这是我的班级:
[XmlRoot("User", Namespace = "http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration")]
public class User
{
public string[]? AlternateEmailAddresses { get; set; }
public string[]? AlternateMobilePhones { get; set; }
public string? AlternativeSecurityIds { get; set; }
public bool? BlockCredential { get; set; }
public string? City { get; set; }
public int? CloudExchangeRecipientDisplayType { get; set; }
public string? Country { get; set; }
public string? Department { get; set; }
public bool? DirSyncEnabled { get; set; }
public string? DirSyncProvisioningErrors { get; set; }
public string? DisplayName { get; set; }
public bool? Errors { get; set; }
public string? Fax { get; set; }
public string? FirstName { get; set; }
public string? ImmutableId { get; set; }
public string? IndirectLicenseErrors { get; set; }
public bool? IsBlackberryUser { get; set; }
public bool? IsLicensed { get; set; }
public string? LastDirSyncTime { get; set; }
public string? LastName { get; set; }
public string? LastPasswordChangeTimestamp { get; set; }
public LicenseAssignmentDetails? LicenseAssignmentDetails { get; set; }
public bool? LicenseReconciliationNeeded { get; set; }
public Licenses? Licenses { get; set; }
public string? LiveId { get; set; }
public string? MSExchRecipientTypeDetails { get; set; }
public string? MSRtcSipDeploymentLocator { get; set; }
public string? MSRtcSipPrimaryUserAddress { get; set; }
public string? MobilePhone { get; set; }
public string? OathTokenMetadata { get; set; }
public string? ObjectId { get; set; }
public string? Office { get; set; }
public string? OverallProvisioningStatus { get; set; }
public bool? PasswordNeverExpires { get; set; }
public bool? PasswordResetNotRequiredDuringActivate { get; set; }
public string? PhoneNumber { get; set; }
public string? PortalSettings { get; set; }
public string? PostalCode { get; set; }
public string? PreferredDataLocation { get; set; }
public string? PreferredLanguage { get; set; }
public object? ProxyAddresses { get; set; }
public string? ReleaseTrack { get; set; }
public ServiceInformation? ServiceInformation { get; set; }
public string? SignInName { get; set; }
public string? SoftDeletionTimestamp { get; set; }
public string? State { get; set; }
public string? StreetAddress { get; set; }
public string? StrongAuthenticationMethods { get; set; }
public string? StrongAuthenticationPhoneAppDetails { get; set; }
public string? StrongAuthenticationProofupTime { get; set; }
public string? StrongAuthenticationRequirements { get; set; }
public string? StrongAuthenticationUserDetails { get; set; }
public bool? StrongPasswordRequired { get; set; }
public string? StsRefreshTokensValidFrom { get; set; }
public string? Title { get; set; }
public string? UsageLocation { get; set; }
public string? UserLandingPageIdentifierForO365Shell { get; set; }
public string? UserPrincipalName { get; set; }
public string? UserThemeIdentifierForO365Shell { get; set; }
public string? UserType { get; set; }
public string? ValidationStatus { get; set; }
public string? WhenCreated { get; set; }
}
public struct LicenseAssignmentDetails
{
public LicenseAssignmentDetail LicenseAssignmentDetail { get; set; }
}
public struct LicenseAssignmentDetail
{
public AccountSku AccountSku { get; set; }
public Assignments Assignments { get; set; }
}
public struct AccountSku
{
public string? AccountName { get; set; }
public string? SkuPartNumber { get; set; }
}
public struct Assignments
{
public LicenseAssignment LicenseAssignment { get; set; }
}
public struct LicenseAssignment
{
[XmlElement("DisabledServicePlans")]
public object? DisabledServicePlans { get; set; }
public string? Error { get; set; }
public string? ReferencedObjectId { get; set; }
public string? Status { get; set; }
}
public struct ServicePlan
{
public string? ServiceName { get; set; }
public string? ServicePlanId { get; set; }
public string? ServiceType { get; set; }
public string? TargetClass { get; set; }
}
public struct ServiceStatus
{
public string? ProvisioningStatus { get; set; }
public ServicePlan? ServicePlan { get; set; }
}
public struct Licenses
{
public UserLicense UserLicense { get; set; }
}
public struct UserLicense
{
public AccountSku AccountSku;
public string? AccountSkuId { get; set; }
[XmlElement("GroupsAssigningLicense", Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
public object? GroupsAssigningLicense { get; set; }
public ServiceStatus[]? ServiceStatus { get; set; }
}
public struct ServiceParameters
{
[XmlElement("ServiceParameter")]
public ServiceParameter ServiceParameter { get; set; }
}
public struct ServiceParameter
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Value")]
public string Value { get; set; }
}
public struct ServiceExtention
{
[XmlElement("ServiceParameters")]
public ServiceParameters ServiceParameters { get; set; }
}
public struct XElement
{
[XmlElement("ServiceExtention", Namespace = "http://schemas.microsoft.com/online/serviceextensions/2009/08/ExtensibilitySchema.xsd")]
public ServiceExtention ServiceExtention { get; set; }
}
public struct ServiceInformation
{
[XmlElement("ServiceInformation")]
public InternalServiceInformation? InternalServiceInformation { get; set; }
}
public struct ServiceElements
{
[XmlElement("XElement")]
public XElement XElement { get; set; }
}
public struct InternalServiceInformation
{
public ServiceElements? ServiceElements { get; set; }
public string? ServiceInstance { get; set; }
}
}所有的房产都是人烟稀少。
唯一未进行填充的propertis是:
ProxyAddresses
ServiceInformation如果我将它声明为一个对象,ProxyAddresses将填充它,但是它通过插入一个属性和一个元素来填充它,这样它就不整洁了,参见:

这就是ServiceInformation给我的东西:

我尝试过各种方法,比如添加XmlArray("ProxyAddresses")和XmlElement("ProxyAddresses"),并将属性设置为string[]和object[],以及一个字符串等等。
下面是需要序列化的原始XML:
<c:ProxyAddresses xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d:string>SMTP:Rainier@M365B654613.OnMicrosoft.com</d:string>
</c:ProxyAddresses> <c:ServiceInformation>
<c:ServiceElements xmlns:d="http://schemas.datacontract.org/2004/07/System.Xml.Linq">
<d:XElement>
<ServiceExtension xmlns="http://schemas.microsoft.com/online/serviceextensions/2009/08/ExtensibilitySchema.xsd">
<ServiceParameters CachePolicy="PerUser">
<ServiceParameter>
<Name>SPO_MySiteUrl</Name>
<Value>https://m365b654613-my.sharepoint.com/personal/emilyb_m365b654613_onmicrosoft_com</Value>
</ServiceParameter>
</ServiceParameters>
</ServiceExtension>
</d:XElement>
</c:ServiceElements>
<c:ServiceInstance>sharepoint/*</c:ServiceInstance>
</c:ServiceInformation>
</c:ServiceInformation>发布于 2022-07-17 10:36:25
这个怎么样?
用于xml
<?xml version="1.0" encoding="UTF-8"?>
<User xmlns:c="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration">
<c:ProxyAddresses xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d:string>SMTP:Rainier@M365B654613.OnMicrosoft.com</d:string>
</c:ProxyAddresses>
<c:ServiceInformation>
<c:ServiceElements xmlns:d="http://schemas.datacontract.org/2004/07/System.Xml.Linq">
<d:XElement>
<ServiceExtension xmlns="http://schemas.microsoft.com/online/serviceextensions/2009/08/ExtensibilitySchema.xsd">
<ServiceParameters CachePolicy="PerUser">
<ServiceParameter>
<Name>SPO_MySiteUrl</Name>
<Value>https://m365b654613-my.sharepoint.com/personal/emilyb_m365b654613_onmicrosoft_com</Value>
</ServiceParameter>
</ServiceParameters>
</ServiceExtension>
</d:XElement>
</c:ServiceElements>
<c:ServiceInstance>sharepoint/*</c:ServiceInstance>
</c:ServiceInformation>
</User>
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.Linq;
namespace ConsoleApp2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(User));
XmlReader reader = XmlReader.Create(FILENAME);
User user = (User)serializer.Deserialize(reader);
}
}
public class User
{
public ProxyAddresses ProxyAddresses { get; set; }
[XmlArray(ElementName = "ServiceInformation", Namespace = "http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration")]
[XmlArrayItem(ElementName = "ServiceElements", Namespace = "http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration")]
public List<ServiceElements> ServiceElements { get; set; }
}
public struct ServiceElements
{
}
public struct ProxyAddresses
{
}
}https://stackoverflow.com/questions/73009314
复制相似问题