简体   繁体   中英

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".

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. Do you have any advice?

XML Source

<?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.

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" . You can probably use these examples to better suit your individual needs.

Use 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";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM