我对C#和XML解析完全陌生,所以我想问一下如何读取这个xml文件中的所有节点?
<root>
<info>
<name>
<first>bob</first>
<last>john</last>
<middle>D</middle>
</name>
<age>35</age>
<sex>male</sex>
<id>12345</id>
</info>
<info>
<name>
<first>jack</first>
<last>dawnson</last>
<middle>D</middle>
</name>
<age>23</age>
<sex>male</sex>
<id>23456</id>
</info>
</root>
我可以获得年龄,性别和身份的价值,但不能获得姓名或其子节点。这是我到目前为止所得到的?
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlOut);
XmlNodeList node = doc.SelectNodes("/root/Info");
Employee empOne = new Employee();
foreach (XmlNode childNode in node)
{
empOne.Age = childNode["age"].InnerText;
empOne.Sex = childNode["sex"].InnerText;
empOne.ID = childNode["id"].InnerText;
foreach (XmlNode node2 in childNode.ChildNodes)
{
empOne.FirstName = node2["first"].InnerText;
empOne.LastName = node2["last"].InnerText;
}
}
答案 0 :(得分:0)
我使用Linq to XML为您的xml创建了一个小示例应用程序。要解析XML,可以使用以下方法:
internal List<Employee> ReadEmployees()
{
List<Employee> employees = new List<Employee>();
string fileName = "XMLFile1.xml";
if (File.Exists(fileName))
{
XDocument document = XDocument.Load(fileName);
if (document.Root != null)
{
foreach (XElement infoElement in document.Root.Elements("info"))
{
Employee employee = new Employee();
XElement ageElement = infoElement.Element("age");
XElement sexElement = infoElement.Element("sex");
XElement idElement = infoElement.Element("id");
XElement nameElement = infoElement.Element("name");
if (ageElement != null)
employee.Age = ageElement.Value;
if (sexElement != null)
employee.Sex = sexElement.Value;
if (idElement != null)
employee.Id = idElement.Value;
if (nameElement != null)
{
var firstnameElement = nameElement.Element("first");
var lastnameElement = nameElement.Element("last");
if (firstnameElement != null)
employee.Firstname = firstnameElement.Value;
if (lastnameElement != null)
employee.Lastname = lastnameElement.Value;
}
employees.Add(employee);
}
}
}
return employees;
}
答案 1 :(得分:0)
我通过创建类来解决这个问题,然后将其转换为xml ..
public class Root
{
public List<Info> infos{ get; set; }
public Root()
{
}
public Root(List<Info> infos)
{
this.infos = new List<Info>();
this.infos = infos;
}
}
public class Info
{
public Name name { get; set; }
public int age { get; set; }
public String sex { get; set; }
public long id { get; set; }
}
public class Name
{
public String first { get; set; }
public String last { get; set; }
public String middle { get; set; }
}
class XmlConvertor
{
public String GetXmlFromObject<T>(T obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
nameSpace.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
StringWriter sr = new StringWriter();
XmlWriter xmlWriter = XmlWriter.Create(sr, settings);
serializer.Serialize(xmlWriter, obj, nameSpace);
return sr.ToString();
}
public T GetObjectFromXml<T>(String xmlString)
{
return (T)new XmlSerializer(typeof(T))
.Deserialize(new StringReader(xmlString));
}
}
在您的主要课程中,您可以使用此方法..
Root root = new XmlConvertor().GetObjectFromXml<Root>("your xml in string");
然后你可以使用这个根变量,并获得你想要的每个值。 我知道它有点冗长,但它更灵活,因为你没有硬编码节点名称
答案 2 :(得分:0)
好吧,我会用一些简单的LINQ2XML来查询它
var xdoc = XDocument.Load(xml);
var p = xdoc.Root.Elements("info").Select(x => new { first = x.Element("name").Element("first").Value,
last = x.Element("name").Element("last").Value,
middle = x.Element("name").Element("middle").Value,
age = x.Element("age").Value,
sex = x.Element("sex").Value, id = x.Element("id").Value});
如果你需要传递它,你必须为它构建一个新对象。