我尝试了很多但是无法从xml字符串中获取值, xml字符串是
<UtilityRateSummaries SchemaVersion="1.0">
<Utility UtilityId="81" UtilityName="Pacific Gas and Electric Company (PG&E)">
<Rate Id="238" Name="Residential Service (Rate E1 Area Y Code B)" Sector="Residential" Metering="OptionalNetMetering" IsDefault="true" IsTimeOfUse="false"/>
</Utility>
</UtilityRateSummaries>
我想要来自这个xml文件的所有值
我正在使用以下课程
[DataContract]
public class getPowerBillRateData
{
[DataMember]
public string UtilityId { get; set; }
[DataMember]
public string UtilityName { get; set; }
[DataMember]
public string RateId { get; set; }
[DataMember]
public string RateName { get; set; }
[DataMember]
public string RateSector { get; set; }
[DataMember]
public string RateMetering { get; set; }
[DataMember]
public string IsDefault { get; set; }
[DataMember]
public string IsTimeofUse { get; set; }
}
我尝试过以下代码,但都没有。
PowerBillRate.UtilityName = xmlDoc.DocumentElement["Rate"].ChildNodes[0].InnerText;
获取xml的代码
XmlDocument xmlDoc = new XmlDocument();
using (var client = new CookieAwareWebClient())
{
Uri uri = new Uri("https://www.mywebserviceurl.com/Services.svc/Rate/" + 238 + "");
client.Credentials = new NetworkCredential("myUsername", "myPassword");
strFetchResData = client.DownloadString(uri);
xmlDoc.LoadXml(strFetchResData);
}
答案 0 :(得分:2)
&
等某些字符有special meaning in XML,您无法直接使用它们。您应该将其替换为&
。如果您确定xml中没有entity references,那么简单的String.Replace
就可以完成这项任务:
xml_string = xml_string.Replace("&", "&");
回到解析。您可以使用LINQ to XML:
var xdoc = XDocument.Parse(xml_string);
var utility = xdoc.Root.Element("Utility");
var data = new getPowerBillRateData();
data.UtilityId = (string)utility.Attribute("UtilityId");
data.UtilityName = (string)utility.Attribute("UtilityName");
var rate = utility.Element("Rate");
data.RateId = (string)rate.Attribute("Id");
data.RateName = (string)rate.Attribute("Name");
data.RateSector = (string)rate.Attribute("Sector");
//etc
另外,我建议您为数据使用更好的命名和更合适的数据类型(整数,布尔值):
[DataContract]
public class Utility
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public Rate Rate { get; set; }
}
[DataContract]
public class Rate
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Sector { get; set; }
[DataMember]
public string Metering { get; set; }
[DataMember]
public bool IsDefault { get; set; }
[DataMember]
public bool IsTimeofUse { get; set; }
}
答案 1 :(得分:0)
XmlSerializer是要走的路,代码比LINQ更清晰,更快。我在这里使用LINQ来打印一个属性的值,IEnumerable可以帮助你获得其余的元素及其属性。
XDocument xDoc = XDocument.Parse(xml);
XElement root = xDoc.Root;
Console.WriteLine(root.Attribute((XName)"SchemaVersion").Value);
IEnumerable<XElement> elements = root.Descendants();
答案 2 :(得分:0)
这取决于您想要对数据做什么,但使用前面提到的'Serializable'可以很容易地处理/处理数据
[Serializable]
[XmlRoot("UtilityRateSummaries")]
public class UtilityRateSummaries
{
[XmlElement("Utility")]
public Utility Utility { get; set; }
[XmlElement("Rate")]
public Rate Rate { get; set; }
}
public class Utility
{
[XmlAttribute]
public string UtilityId { get; set; }
[XmlAttribute]
public string UtilityName { get; set; }
}
public class Rate
{
[XmlAttribute]
public string Id { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Sector { get; set; }
[XmlAttribute]
public string Metering { get; set; }
[XmlAttribute]
public string IsDefault { get; set; }
[XmlAttribute]
public string IsTimeOfUse { get; set; }
}
取消序列化的方法:
public static void Serialize(string filename, UtilityRateSummaries objToXMl)
{
var xs
= new XmlSerializer(objToXMl.GetType());
var writer = File.CreateText(filename);
xs.Serialize(writer, objToXMl);
writer.Flush();
writer.Close();
}
public static UtilityRateSummaries Deserialize(string filename)
{
var xs
= new XmlSerializer(
typeof(UtilityRateSummaries));
var reader = File.OpenText(filename);
var utilityRateSummaries = (UtilityRateSummaries)xs.Deserialize(reader);
reader.Close();
return utilityRateSummaries;
}
static void Main(string[] args)
{
var obj1 = new UtilityRateSummaries();
obj1.Utility = new Utility();
obj1.Utility.UtilityId = 81;
obj1.Utility.UtilityName = "Pacific Gas and Electric Company (PG&E)";
obj1.Rate = new Rate();
obj1.Rate.Id = 238;
obj1.Rate.Name = "Residential Service (Rate E1 Area Y Code B)";
obj1.Rate.IsDefault = true;
obj1.Rate.IsTimeOfUse = false;
obj1.Rate.Metering = "OptionalNetMetering";
obj1.Rate.Name = "1";
obj1.Rate.Sector = "Residential";
Serialize("bla.xml",obj1);
var obj = Deserialize("bla.xml");
Console.Out.WriteLine(obj.Rate.Id);
}
PS:你的XML无效,也可能是个问题!