所以我无法弄清楚如何做到这一点,我将从我从其他API获得的xml格式开始(简化)
<exportData encryptedPhrase="stuff" version="1" stVersion="1">
<completeAdministrator>
<administratorData id="1">
<name>stuff</name>
<operations>0</operations>
<administrativeRole>Some role</administrativeRole>
<encryptedPassphrase>pass</encryptedPassphrase>
<isChangePassword>false</isChangePassword>
<isDualAuthRequired>false</isDualAuthRequired>
<lastLoginTime>2014-12-23T15:00:27-08:00</lastLoginTime>
<loginFailureCount>0</loginFailureCount>
<lastFailureLoginTime>2014-12-23T14:54:42-08:00</lastFailureLoginTime>
<isLockedOut>false</isLockedOut>
<lastPasswordChangeTime>2014-12-23T15:04:38-08:00</lastPasswordChangeTime>
<customAttributes>
<customProperties>
</customProperties>
<localCertificates>
</localCertificates>
<partnerCertificates>
</partnerCertificates>
<userCertificates>
</userCertificates>
</customAttributes>
</administratorData>
<businessUnit>bu</businessUnit>
<parentAdministratorName>admin</parentAdministratorName>
</completeAdministrator>
</exportData>
所以基本上我想要一个包含管理对象列表(mainAdministrator)的主类(exportData),每个管理对象都有一个adminstratordata对象和一个自定义属性对象。
我使用了“粘贴特殊”功能让.Net为我创建了一个似乎有效的类,但是当我使用此代码时,只读入了第一个项目。如何使其工作?
XmlSerializer deserializer = new XmlSerializer(typeof(AdminData));
FileStream fs = new FileStream(@"C:\Projects\SafeXMLImport\ST 5.2.1 export Data & document\adm.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
AdminData adminXml = (AdminData)deserializer.Deserialize(reader);
答案 0 :(得分:0)
尝试this创建类,有时VisualStudio不知道创建正确的类。我认为你的xml应该和开头的标签一样。
答案 1 :(得分:0)
这是我最终做的事情。我必须创建一个businessUnits数组,因为我发现在一个条目中可以有多个标记。
XDocument doc = XDocument.Load(@"C:\Projects\SafeXMLImport\ST 5.2.1 export Data & document\adm.xml");
List < CompleteAdministrator > adminList =
(
from c in doc.Root.Elements("completeAdministrator")
select new CompleteAdministrator
{
businessUnits = (from d in c.Elements("businessUnit")
select d.Value).ToArray(),
parentAdministratorName = (string)c.Element("parentAdministratorName"),
administratorData =
(
new AdministratorData
{
id = (string)c.Element("administratorData").Attribute("id"),
name = (string)c.Element("administratorData").Element("name"),
operations = (string)c.Element("administratorData").Element("operations"),
administrativeRole = (string)c.Element("administratorData").Element("administrativeRole"),
encryptedPassphrase = (string)c.Element("administratorData").Element("encryptedPassphrase"),
isChangePassword = (string)c.Element("administratorData").Element("isChangePassword"),
isDualAuthRequired = (string)c.Element("administratorData").Element("isDualAuthRequired"),
lastLoginTime = (string)c.Element("administratorData").Element("lastLoginTime"),
loginFailureCount = (string)c.Element("administratorData").Element("loginFailureCount"),
lastFailureLoginTime = (string)c.Element("administratorData").Element("lastFailureLoginTime"),
isLockedOut = (string)c.Element("administratorData").Element("isLockedOut"),
lastPasswordChangeTime = (string)c.Element("administratorData").Element("lastPasswordChangeTime")
}
)
}
).ToList();