如何使用c#获取根元素的所有键值?
How to get all the key-values of the root element using c#?
我搜索了几天如何解析我的 xml 文件。
所以我的问题是我想恢复根元素的所有键值。
文件示例:
<?xml version="1.0" ?>
<!DOCTYPE ....... SYSTEM ".....................">
<coverage x="y1" x2="y2" x3="y3" x4="y4">
<sources>
<source>.............</source>
</sources>
.....
<\coverage>
在这里,我要恢复 "coverage" 的所有值:x1 和他的值,x2 和他的值,x3 和他的值 x3 ...
我已经尝试将 "XmlReader" 与我能找到的所有教程一起使用,但它仍然不起作用。
我尝试过的所有教程都恢复了某个节点(标签)中的值,但从未恢复根元素的所有值。
也许已经存在具有相同问题的教程,但我没有找到他。
预先感谢您的帮助。
您可以使用 XElement
并执行此操作。
XElement element = XElement.Parse(input);
var results = element.Attributes()
.Select(x=>
new
{
Key = x.Name,
Value = (string)x.Value
});
输出
{ Key = x, Value = y1 }
{ Key = x2, Value = y2 }
{ Key = x3, Value = y3 }
{ Key = x4, Value = y4 }
勾选这个Demo
//Use System.Xml namespace
//Load the XML into an XmlDocument object
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strPathToXmlFile); //Physical Path of the Xml File
//or
//xDoc.LoadXml(strXmlDataString); //Loading Xml data as a String
//In your xml data - coverage is the root element (DocumentElement)
XmlNode rootNode = xDoc.DocumentElement;
//To get all the attributes and its values
//iterate thru the Attributes collection of the XmlNode object (rootNode)
foreach (XmlAttribute attrib in rootNode.Attributes)
{
string attributeName = attrib.Name; //Name of the attribute - x1, x2, x3 ...
string attributeValue = attrib.Value; //Value of the attribute
//do your logic here
}
//if you want to save the changes done to the document
//xDoc.Save (strPathToXmlFile); //Pass the physical path of the xml file
rootNode = null;
xDoc = null;
希望对您有所帮助。
我搜索了几天如何解析我的 xml 文件。 所以我的问题是我想恢复根元素的所有键值。
文件示例:
<?xml version="1.0" ?>
<!DOCTYPE ....... SYSTEM ".....................">
<coverage x="y1" x2="y2" x3="y3" x4="y4">
<sources>
<source>.............</source>
</sources>
.....
<\coverage>
在这里,我要恢复 "coverage" 的所有值:x1 和他的值,x2 和他的值,x3 和他的值 x3 ... 我已经尝试将 "XmlReader" 与我能找到的所有教程一起使用,但它仍然不起作用。 我尝试过的所有教程都恢复了某个节点(标签)中的值,但从未恢复根元素的所有值。
也许已经存在具有相同问题的教程,但我没有找到他。
预先感谢您的帮助。
您可以使用 XElement
并执行此操作。
XElement element = XElement.Parse(input);
var results = element.Attributes()
.Select(x=>
new
{
Key = x.Name,
Value = (string)x.Value
});
输出
{ Key = x, Value = y1 }
{ Key = x2, Value = y2 }
{ Key = x3, Value = y3 }
{ Key = x4, Value = y4 }
勾选这个Demo
//Use System.Xml namespace
//Load the XML into an XmlDocument object
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strPathToXmlFile); //Physical Path of the Xml File
//or
//xDoc.LoadXml(strXmlDataString); //Loading Xml data as a String
//In your xml data - coverage is the root element (DocumentElement)
XmlNode rootNode = xDoc.DocumentElement;
//To get all the attributes and its values
//iterate thru the Attributes collection of the XmlNode object (rootNode)
foreach (XmlAttribute attrib in rootNode.Attributes)
{
string attributeName = attrib.Name; //Name of the attribute - x1, x2, x3 ...
string attributeValue = attrib.Value; //Value of the attribute
//do your logic here
}
//if you want to save the changes done to the document
//xDoc.Save (strPathToXmlFile); //Pass the physical path of the xml file
rootNode = null;
xDoc = null;
希望对您有所帮助。