我对使用XML文件中的Attributes做同样的事情没有任何问题。我能够通过以下方式这样做:
我声明了我的XML文档
static XDocument cfgXml;
我单击一个按钮并浏览到我的XML文件并选择它。
private void selcFile_Click(object sender, EventArgs e)
{
DialogResult result1 = openFileDialog1.ShowDialog();
string xmlFile = openFileDialog1.FileName;
if (result1 == DialogResult.OK)
{
cfgXml = XDocument.Load(@xmlFile);
}
}
我有一个类来创建List
class DiskPoolDisks
{
public string id { get; set; }
public DiskPoolDisks(string a)
{
this.id = a;
}
}
我点击按钮加载并创建我的列表
private void loadCfg_Click(object sender, EventArgs e)
{
List<DiskPoolDisks> pooldiskArray = new List<DiskPoolDisks>(from d in cfgXml.XPathSelectElements("//Configuration//ServerGroup//Servers//Server//DiskPools// PhysicalDisks//PhysicalDisks") select new DiskPoolDisks((string)d.Attribute("id")));
}
现在我需要做一个不同的XML文件,其中包含我需要从其值中创建List的元素。
然而,尝试使用上面相同的方法不起作用,我得到一个计数为零的列表,其中我应该在列表中至少有一个值。
static XDocument objXml;
private void selcObjFile_Click(object sender, EventArgs e)
{
DialogResult result2 = openFileDialog2.ShowDialog();
string xmlFile = openFileDialog2.FileName;
if (result2 == DialogResult.OK)
{
objXml = XDocument.Load(@xmlFile);
}
}
private void loadCfg_Click(object sender, EventArgs e)
{
List<CdpHistory> cdphistoryArray = new List<CdpHistory>(from d in objXml.XPathSelectElements("//DataRepository//StreamLogicalDiskData//MinQuota")
select new CdpHistory((string)d.Attribute("Value")));
}
我正在尝试从以下XML文件中的MinQuota元素获取值的XML文件
<Repository>
<StreamData>
<SequenceNumber>63066</SequenceNumber>
<Id>188804e8-c579-438c-8a17-1d1f2ce89d17</Id>
<Caption>Caption goes here</Caption>
<ExtendedCaption>Log history </ExtendedCaption>
<Internal>false</Internal>
<Server>949303A9-8472-4979-9EA0-A807831574AE</Server>
<DataStatus>Undefined</DataStatus>
<PresenceStatus>Present</PresenceStatus>
<Size>
<Value>1125899906842624</Value>
</Size>
<MappingName>mapping one</MappingName>
<Status>Online</Status>
<Virtualized>false</Virtualized>
<AccessRights>NotDefined</AccessRights>
<Failure>Undefined</Failure>
<Role>Unknown</Role>
<IsMapped>false</IsMapped>
<Protected>true</Protected>
<Id>949303A9</Id>
<Index>10</Index>
<MinQuota>
<Value>11811160064</Value>
</MinQuota>
<MaxQuota>
<Value>0</Value>
</MaxQuota>
<Affinity>
<int>1</int>
<int>2</int>
<int>3</int>
</Affinity>
<State>Idle</State>
<Time>0</Time>
<Size>
<Value>0</Value>
</Size>
<State>NotPresent</State>
</StreamData>
</Repository>
答案 0 :(得分:0)
事实证明XML文件中有一个命名空间,所以我不得不添加以下内容:
private void selcObjFile_Click(object sender, EventArgs e)
{
DialogResult result2 = openFileDialog2.ShowDialog();
string xmlFile = openFileDialog2.FileName;
if (result2 == DialogResult.OK)
{
objXml = XDocument.Load(@xmlFile);
nsm.AddNamespace("dat", "http://www.abc.com/");
nodeList = root.SelectNodes("//dat:StreamData/dat:MinQuota", nsm);
}
}
完成后,以下允许我创建我需要的列表:
List<string> cdphistoryArray = new List<string>();
foreach (XmlNode MinQuota in nodeList)
{
if (MinQuota.LastChild.InnerText != "0" && MinQuota.LastChild.InnerText != null)
{
cdphistoryArray.Add(Convert.ToString(MinQuota.LastChild.InnerText));
}
}