[英]Read and edit XML value
I want to read and edit the Value string (Value="J:\\Demo\\Demo_Data_3.xml") next to Parameter Name="database". 我想读取和编辑参数名称=“数据库”旁边的值字符串(Value =“ J:\\ Demo \\ Demo_Data_3.xml”)。
When I use 当我使用
XPathDocument xPathDoc = new XPathDocument(dashboardPath);
XPathNavigator navigator = xPathDoc.CreateNavigator();
while (navigator.MoveToFollowing("Parameters", ""))
I can move to to <Parameter>
but not read or edit the values. 我可以移至
<Parameter>
但不能读取或编辑值。 Do you have any advice? 您有什么建议吗?
XML Source XML来源
<?xml version="1.0" encoding="utf-8"?>
<Dashboard CurrencyCulture="en-US">
<Title Text="Dashboard" />
<DataSources>
<SqlDataSource ComponentName="dashboardSqlDataSource1">
<Name>Demo_Data_Excel</Name>
<Connection Name="testdata" ProviderKey="InMemorySetFull">
<Parameters>
<Parameter Name="database" Value="J:\Demo\Demo_Data_3.xml" />
<Parameter Name="read only" Value="1" />
<Parameter Name="generateConnectionHelper" Value="false" />
</Parameters>
</Connection>
<Query Type="TableQuery" Name="Data">
<Table Name="Data">
<Column Name="Market Segment" />
<Column Name="Market Subsegmt" />
<Column Name="Customer" />
</Table>
</Query>
<ResultSchema>
<DataSet Name="SQL Data Source 1">
<View Name="Data">
<Field Name="Market Segment" Type="String" />
<Field Name="Market Subsegmt" Type="String" />
</View>
</DataSet>
</ResultSchema>
</SqlDataSource>
</DataSources>
</Dashboard>
Two possible ways of doing this given you have a file called TestFile.xml
next to the program's exe. 如果在程序的exe旁边有一个名为
TestFile.xml
的文件,则有两种可能的方法。
First: 第一:
var xDocument = XDocument.Load("TestFile.xml");
xDocument.XPathSelectElement(
"/Dashboard/DataSources/SqlDataSource/Connection/Parameters/Parameter[@Name='database']")
.Attribute("Value").SetValue("UpdatedPath");
xDocument.Save("TestFile_updated.xml");
Second: 第二:
var xDocument = XDocument.Load("TestFile.xml");
xDocument.Root.Elements("DataSources")
.Elements("SqlDataSource")
.Elements("Connection")
.Elements("Parameters")
.Elements("Parameter")
.Single(p => p.Attribute("Name").Value == "database")
.Attribute("Value")
.SetValue("UpdatedPath");
xDocument.Save("TestFile_updated.xml");
The second one assumes you will always have exactly one Parameter
node where Name="database"
. 第二个假设您将始终只有一个
Parameter
节点,其中Name="database"
。 You can probably use these examples to better suit your individual needs. 您可能可以使用这些示例来更好地满足您的个人需求。
Use XML Linq 使用XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Dashboard CurrencyCulture=\"en-US\">" +
"<Title Text=\"Dashboard\" />" +
"<DataSources>" +
"<SqlDataSource ComponentName=\"dashboardSqlDataSource1\">" +
"<Name>Demo_Data_Excel</Name>" +
"<Connection Name=\"testdata\" ProviderKey=\"InMemorySetFull\">" +
"<Parameters>" +
"<Parameter Name=\"database\" Value=\"J:\\Demo\\Demo_Data_3.xml\" />" +
"<Parameter Name=\"read only\" Value=\"1\" />" +
"<Parameter Name=\"generateConnectionHelper\" Value=\"false\" />" +
"</Parameters>" +
"</Connection>" +
"<Query Type=\"TableQuery\" Name=\"Data\">" +
"<Table Name=\"Data\">" +
"<Column Name=\"Market Segment\" />" +
"<Column Name=\"Market Subsegmt\" />" +
"<Column Name=\"Customer\" />" +
"</Table>" +
"</Query>" +
"<ResultSchema>" +
"<DataSet Name=\"SQL Data Source 1\">" +
"<View Name=\"Data\">" +
"<Field Name=\"Market Segment\" Type=\"String\" />" +
"<Field Name=\"Market Subsegmt\" Type=\"String\" />" +
"</View>" +
"</DataSet>" +
"</ResultSchema>" +
"</SqlDataSource>" +
"</DataSources>" +
"</Dashboard>";
XDocument doc = XDocument.Parse(xml);
XElement database = doc.Descendants("Parameter").Where(x => (string)x.Attribute("Name") == "database").FirstOrDefault();
database.Attribute("Value").Value = "J:\\Demo\\Demo_Data_4.xml";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.