110,571
社区成员
发帖
与我相关
我的任务
分享
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
static public void ModifyPrice(string filename, double discount)
{
XDocument Doc = XDocument.Load(filename);
var Query = from D in Doc.Element("bookstore").Elements("book")
where D.Element("title").Value.IndexOf(filename) > -1
select D;
foreach (var v in Query)
{//没有进入???
v.Element("price").Value = (Convert.ToDouble(v.Element("price").Value) * discount).ToString();
}
Doc.Save(filename);
}
public class Book
{
public string genre { get; set; }
public string publicationdate { get; set; }
public string ISBN { get; set; }
public string title { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string price { get; set; }
}
//你的filename对应xml哪个?我对应title做测试
public static void ModifyPrice(string filename, double discount)
{
XDocument Doc = XDocument.Load(@"C:\books1.xml");
var Query = from D in Doc.Element("bookstore").Elements("book")
where D.Element("title").Value.IndexOf(filename) > -1
select D;
foreach (var v in Query)
{
v.Element("price").Value = (Convert.ToDouble(v.Element("price").Value) * discount).ToString();
}
Doc.Save(@"C:\books1.xml");
}
public static bool DeleteBook(string strTitle)
{
bool Result = false;
try
{
XDocument Doc = XDocument.Load(@"C:\books1.xml");
Doc.Element("bookstore").Elements("book").Where(x => x.Element("title").Value == strTitle).Select(x => x).Remove();
Doc.Save(@"C:\books1.xml");
Result = true;
Console.WriteLine(Doc.ToString());
}
catch
{
}
finally
{
}
return Result;
}
public static void InsertBook(Book Book)
{
XDocument Doc = XDocument.Load(@"C:\books1.xml");
XElement node=new XElement("book",new XAttribute("genre",Book.genre),
new XAttribute("publicationdate",Book.publicationdate),
new XAttribute("ISBN",Book.ISBN),
new XElement("title",Book.title),
new XElement("author",
new XElement("first-name",Book.first_name),
new XElement("last-name"),Book.last_name),
new XElement("price",Book.price));
Doc.Element(ElementNode).Add(node);
Doc.Save(XmlPath);
}
static void Main(string[] args)
{
ModifyPrice("Autobiography", 0.9);
Console.WriteLine(XDocument.Load(@"C:\books1.xml").ToString());
DeleteBook("The Confidence Man");
Console.ReadKey();
XDocument doc=XDocument.Load("C:\\books1.xml");
List<string> PList = new List<string>();
var query = from price in doc.Descendants("price") select price.Value;
foreach (var item in query)
{
PList.Add(item);
}
doc.Save("C:\\books1.xml");
XDocument doc=XDocument.Load("C:\\books1.xml");
XElement xe = null;
var query = from title in doc.Descendants("title") where title.Value == "值" && title.Parent.Name == "book" select title.Parent;
foreach (var item in query)
{
xe = item;
}
xe.Remove();
doc.Save("C:\\books1.xml");