<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="t3.xsd">
<book id="001" type="IT">
<bookName>mastering XML</bookName>
<author>Jack LEE</author>
<ISBN>0-125-11111-6</ISBN>
<price>36.00</price>
<quantity>6</quantity>
</book>
<book id="002" type="education">
<bookName>the method of study</bookName>
<author>Thomas</author>
<ISBN>0-125-22222-7</ISBN>
<price>45.00</price>
<quantity>5</quantity>
</book>
<book id="003" type="novel">
<bookName>The hero</bookName>
<author>TuDou</author>
<ISBN>0-125-88888-6</ISBN>
<price>88.00</price>
<quantity>2</quantity>
</book>
</books> id,type需要转成字段,这段格式就是调用别人Service返回的结果,他们不可能去改结果的。
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="t3.xsd">
<book id="001" type="IT">
<bookName>mastering XML</bookName>
<author>Jack LEE</author>
<ISBN>0-125-11111-6</ISBN>
<price>36.00</price>
<quantity>6</quantity>
</book>
<book id="002" type="education">
<bookName>the method of study</bookName>
<author>Thomas</author>
<ISBN>0-125-22222-7</ISBN>
<price>45.00</price>
<quantity>5</quantity>
</book>
<book id="003" type="novel">
<bookName>The hero</bookName>
<author>TuDou</author>
<ISBN>0-125-88888-6</ISBN>
<price>88.00</price>
<quantity>2</quantity>
</book>
</books> id,type需要转成字段,这段格式就是调用别人Service返回的结果,他们不可能去改结果的。
解决方案 »
- gridcontrol 获取 分组 统计值?
- C#调用DLL,出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
- 如何获取打印机的内置字体?紧急!!!!!!!
- DataGridView某个动态数据以实时监控方式输出
- c# 中怎样才可以将一个treeview实例真正保存在另一个treeview实例中?一定结贴!
- 状态是怎么传递的?
- 数据填充问题
- 现在有一个XSD文件,在C#中能得到其对应的类吗
- 关于在C#中将数据导出为dbf文件的问题
- 对web应用程序c#如何实现设置期限,到期限报警啊(在线)
- 求助!!C#中如何将DataGridView中某一列中的值求和,将得到的和显示到一个TextBox中?
- winform Socket的一个问题
XMLDocument
XElement
2种方法都可以
{
[XmlElement("book")]
public book[] book { get; set; }
}
public class book
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
public string bookName { get; set; }
public string author { get; set; }
public string ISBN { get; set; }
public double price { get; set; }
public int quantity { get; set; }
}
string str = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<books xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xsi:noNamespaceSchemaLocation=""t3.xsd"">
<book id=""001"" type=""IT"">
<bookName>mastering XML</bookName>
<author>Jack LEE</author>
<ISBN>0-125-11111-6</ISBN>
<price>36.00</price>
<quantity>6</quantity>
</book>
<book id=""002"" type=""education"">
<bookName>the method of study</bookName>
<author>Thomas</author>
<ISBN>0-125-22222-7</ISBN>
<price>45.00</price>
<quantity>5</quantity>
</book>
<book id=""003"" type=""novel"">
<bookName>The hero</bookName>
<author>TuDou</author>
<ISBN>0-125-88888-6</ISBN>
<price>88.00</price>
<quantity>2</quantity>
</book>
</books>";
books books = new books();
XmlSerializer xml = new XmlSerializer(books.GetType());
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str));
books = xml.Deserialize(ms) as books;
ms.Dispose();
foreach (book b in books.book)
Console.WriteLine("{0}={1}={2}={3}={4}={5}={6}", b.id, b.type, b.bookName, b.author, b.ISBN, b.price.ToString(), b.quantity);
/*
001=IT=mastering XML=Jack LEE=0-125-11111-6=36=6
002=education=the method of study=Thomas=0-125-22222-7=45=5
003=novel=The hero=TuDou=0-125-88888-6=88=2*/
貌似例子的xml结构不够复杂
马上尝试一下,如果book里面还内嵌一个比较出版社的详细信息该如何修改呢
<status>success</status>
<Inventory id="246354">
<response id="12345">
Deleted
</response>
<response id="123456">
Deleted
</response>
</Inventory>
</FinalResults>红色部分是Service返回的统一格式,连 XML头都没有了,楼上兄弟提供的几招似乎都用不上了。
xml.LoadXml(@"<FinalResults curapiversion=""1.6.1"" Testappversion=""1.5"" responsetime=""0.0111111"">
<status>success</status>
<Inventory id=""246354"">
<response id=""12345"">
Deleted
</response>
<response id=""123456"">
Deleted
</response>
</Inventory>
</FinalResults>");
XmlNode status = xml.SelectSingleNode("/FinalResults/status");
Console.WriteLine(status.InnerText);
XmlNodeList responseID = xml.SelectNodes("/FinalResults/Inventory/response");
foreach (XmlNode n in responseID)
Console.WriteLine(n.Attributes["id"].Value);
/*
其他节点类似这样取。
success
12345
123456*/
public class books
{
[XmlArray]
[XmlArrayItem("book", typeof(book))]
public book[] book { get; set; }
}
public class book
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
[XmlElement]
public string bookName { get; set; }
[XmlElement]
public string author { get; set; }
[XmlElement]
public string ISBN { get; set; }
[XmlElement]
public double price { get; set; }
[XmlElement]
public int quantity { get; set; }
}
2、加入重载属性定义: private void button6_Click(object sender, EventArgs e)
{
try
{
XmlAttributes attrs = new XmlAttributes();
attrs.XmlElements.Add(new XmlElementAttribute("book", typeof(book)));
XmlAttributeOverrides attrOver = new XmlAttributeOverrides();//这玩意是控制Xml重新输出和加载
attrOver.Add(typeof(Inventroty), "book", attrs);
FileStream fs = new FileStream(@"c:\test.xml", FileMode.Open);
XmlSerializer sr= new XmlSerializer(typeof(books),attrOver);
books bs= (Inventroty)sr.Deserialize(fs);
fs.Close(); }
catch (Exception ex)
{ }
}