我有XML格式的數據。我將它存儲在varchar數據類型列中。我通過在Visual Studio 2010中使用Linq to sql來檢索它。我在字符串變量中獲得了xml格式的數據。現在我需要閱讀它作爲一個Xml。我需要在特定的節點中獲得價值。從字符串中讀取XML
for example,
<Sale>
<LTV>150</LTV>
<CLTV>350</CLTV>
<DLTV>600</DLTV>
</sale>
我需要接受CLTV的價值。
我有XML格式的數據。我將它存儲在varchar數據類型列中。我通過在Visual Studio 2010中使用Linq to sql來檢索它。我在字符串變量中獲得了xml格式的數據。現在我需要閱讀它作爲一個Xml。我需要在特定的節點中獲得價值。從字符串中讀取XML
for example,
<Sale>
<LTV>150</LTV>
<CLTV>350</CLTV>
<DLTV>600</DLTV>
</sale>
我需要接受CLTV的價值。
嘗試
var xml = XElement.Parse("your xml");
//Gives you the value of the CLTV node
xml.Descendants("CLTV").FirstOrDefault().Value;
要改變數值
xml.Descendants("CLTV").FirstOrDefault().Value = "1";
//Save to disk
xml.Save({stream or file location});
//Get a string back
xml.ToString();
後人會給你XElements的列表,你可以枚舉或做FirstOrDefault你會得到的第一個找到或空元件。
此代碼應爲你工作:
using System.Xml;
...
string xmlStr = "<sale><LTV>150</LTV><CLTV>350</CLTV><DLTV>600</DLTV></sale>";
XmlDocument x = new XmlDocument();
x.LoadXml(xmlStr);
MessageBox.Show(x.GetElementsByTagName("CLTV")[0].InnerText);
var value = XDocument.parse("YOUR_XML_STRING").Root.Element("ELEMENT_NAME").Value;
見XmlDocument的或的XDocument –