[C#]我需要帮助Xml [英] [C#] I Need Help With Xml

查看:85
本文介绍了[C#]我需要帮助Xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我需要快速完成这个,所以我没有时间搜索谷歌,所以如果有人可以快速帮助,这就是:



i有这个小xml文件,



 <?  xml     version   =  1.0   编码  =  UTF-8  >  

< volume >
< player &g t;
< volume 名称 = vol1 > 75 < / volume >
< volume name = vol2 > 50 < / volume >
< volume name = vol3 > 100 < / volume >
< / players >
< / volumes >





i需要以编程方式更改这些卷名的值,例如,我输入参数vol2,新值为70,我需要改变,50到70,我该怎么做? tnx提前。



试过类似的东西:



  string  filepath = Settings.GetProjectDirectory()+   \\ Data\\volumes.xml; 
XDocument xdoc = XDocument.Load(filepath);
var element = xdoc.Elements( vol2)。 Single ();
element.Value = Volume.ToString();
xdoc.Save(filepath);





但我收到错误..序列中没有元素..

解决方案

使用您给定的结构,尝试类似于:

  var  element = xdoc.Elements(  volume)。(v = >  v.Attributes(  name)。any(a = >  a.Value ==   vol2)); 



这可能不是特别有效!!! br />
BTW:如果没有元素 name ==vol2 .Single 将抛出异常!





来自Maciej解决方案的借款:

< pre lang =c#> var element = xdoc.Descendents( volume)。(v = > string )v。属性( name)== vol2);


试试这个:

  var  element = xdoc.Descendants(  volume)。其中(x = >  string )x.Attribute (  name)==   vol2)。FirstOrDefault(); 





完成LinqPad代码:

  str ing  xcontent =  @ <?xml version ='1.0'coding ='UTF-8'? > 
< volumes>
< players>
< volume name ='vol1'> 75< / volume>
< volume name ='vol2'> 50< / volume>
< volume name ='vol3'> 100< / volume>
< / players>
< / volumes>
;

XDocument xdoc = XDocument.Parse(xcontent);

var xelement = xdoc.Descendants( volume)。其中(x = < span class =code-keyword>> ( string )x.Attribute( name)== vol2)。FirstOrDefault();

xelement.Dump();





返回:

 <   volume     name   =  vol2  >  50 <   / volume  >  


你做错了。您的文档只包含一个元素卷。顺便说一句,XML只允许一个根元素。



目前尚不清楚改变价值是什么意思。哪里?如果需要更改文件本身的值,则必须解析文件并使用所需的更改再次编写。 .NET FCL为XML解析/生成提供了不同的方法。这是我对它们的简短概述:

  1. 使用 System.Xml.XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
  2. 使用类 System.Xml .XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx http://msdn.microsoft.com/en-us/library/bb387063.aspx




  4. 我不确定你是否真的需要直接使用.NET。很可能最好使用序列化,它会在XML(或JSON)形式的某些流中自动保存某些对象图,并在以后将其恢复到内存中。请参阅:

    https://en.wikipedia.org/wiki/Serialization [< a href =https://en.wikipedia.org/wiki/Serializationtarget =_ blanktitle =New Window> ^ ],

    https://msdn.microsoft.com/en-us/library/ms233843.aspx [ ^ ]。



    最先进,最强大,非侵入性和最易于使用的序列化方法是 Method Contract 。请参阅: https://msdn.microsoft.com/en-us /library/ms733127%28v=vs.110%29.aspx [ ^ ]。



    -SA


first of all, i need to finish this really fast so i dont have a time searching on google, so if there is anyone for quick help, this is it:

i have this little xml file,

<?xml version="1.0" encoding="UTF-8"?>

<volumes>
  <players>
      <volume name="vol1">75</volume>
	  <volume name="vol2">50</volume>
	  <volume name="vol3">100</volume>
  </players>
</volumes>



i need programatically to change the value of these volume names, for example, i have input parameter "vol2" with new value of 70, and i need to change, 50 to 70, how can i do this? tnx in advance.

tried something like:

string filepath = Settings.GetProjectDirectory() + "\\Data\\volumes.xml";
XDocument xdoc = XDocument.Load(filepath);
var element = xdoc.Elements("vol2").Single();
element.Value = Volume.ToString();
xdoc.Save(filepath);



but i get an error .. "sequence contains no elements" ..

解决方案

With your given structure, try something like:

var element = xdoc.Elements("volume").Single(v => v.Attributes("name").Any(a => a.Value == "vol2"));


This probably is not particularly efficient!!!
BTW: if there is no element with name == "vol2" the .Single will throw an exception!

[Edit: MTH]
"Borrowing" from Maciej's solution:

var element = xdoc.Descendents("volume").Single(v => (string)v.Attribute("name") == "vol2");


Try this:

var element = xdoc.Descendants("volume").Where(x=>(string)x.Attribute("name")=="vol2").FirstOrDefault();



Complete LinqPad code:

string xcontent =@"<?xml version='1.0' encoding='UTF-8'?>
<volumes>
  <players>
      <volume name='vol1'>75</volume>
      <volume name='vol2'>50</volume>
      <volume name='vol3'>100</volume>
  </players>
</volumes>";

XDocument xdoc = XDocument.Parse(xcontent);

var xelement = xdoc.Descendants("volume").Where(x=>(string)x.Attribute("name")=="vol2").FirstOrDefault();

xelement.Dump();



Returns:

<volume name="vol2">50</volume>


You are doing it wrong. Your document contains only one element, "volumes". XML allows only one root element, by the way.

It's not clear what you mean by "changing value". Where? If your need to change the value in the file itself, you have to parse the file and write it again, with the changes you need. .NET FCL offers different ways for XML parsing/generation. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx, http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.



I'm not sure you really need to work with .NET directly. It's pretty likely that it would be better to use serialization which automatically persist some object graph in some stream in XML (or JSON) form and restores it back to memory later. Please see:
https://en.wikipedia.org/wiki/Serialization[^],
https://msdn.microsoft.com/en-us/library/ms233843.aspx[^].

The most advanced, robust, non-intrusive and easiest to use approach to serialization is Method Contract. Please see: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

—SA


这篇关于[C#]我需要帮助Xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆