我想读取和编辑Value字符串(Value =“J:\ Demo \ Demo_Data_3.xml”) 在参数名称=“数据库”旁边。
当我使用
时XPathDocument xPathDoc = new XPathDocument(dashboardPath);
XPathNavigator navigator = xPathDoc.CreateNavigator();
while (navigator.MoveToFollowing("Parameters", ""))
我可以转到<Parameter>
但不会读取或编辑值。
你有什么建议吗?
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>
答案 0 :(得分:0)
两种可能的方法,因为程序的exe旁边有一个名为TestFile.xml
的文件。
首先:
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");
第二
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");
第二个假设您将始终只有一个Parameter
节点Name="database"
。您可以使用这些示例来更好地满足您的个人需求。
答案 1 :(得分:0)
使用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";
}
}
}