写一个js从xml文件里读取name为txtSC的textbox中的值
时间: 2024-02-27 07:57:25 浏览: 27
假设已经通过XMLHttpRequest获取到了XML文件,并且XML文件内容如下:
```xml
<root>
<textbox name="txtSC">90</textbox>
<textbox name="txtMC">80</textbox>
</root>
```
可以通过以下代码获取name为"txtSC"的textbox标签的值:
```javascript
const xhr = new XMLHttpRequest();
xhr.open('GET', 'your-xml-file.xml', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const xmlDoc = xhr.responseXML;
const txtSCNode = xmlDoc.querySelector('textbox[name="txtSC"]');
const txtSCValue = txtSCNode.textContent;
console.log(txtSCValue); // 输出90
}
}
xhr.send();
```
在以上代码中,我们通过`querySelector`方法获取到name为"txtSC"的textbox标签节点,并通过`textContent`属性获取该节点的值。最后将该值输出到控制台。
相关问题
用wpf 写一个读取xml文件的程序
以下是一个使用WPF读取XML文件的示例程序:
MainWindow.xaml
```xml
<Window x:Class="WpfXmlReader.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XML Reader" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBlock Text="XML File Path:"/>
<TextBox x:Name="txtXmlFilePath"/>
<Button Content="Read XML" Click="btnReadXml_Click"/>
<ListBox x:Name="lstXmlData"/>
</StackPanel>
</Grid>
</Window>
```
MainWindow.xaml.cs
```csharp
using System.Windows;
using System.Xml;
namespace WpfXmlReader
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnReadXml_Click(object sender, RoutedEventArgs e)
{
lstXmlData.Items.Clear();
string xmlFilePath = txtXmlFilePath.Text.Trim();
if (!string.IsNullOrEmpty(xmlFilePath))
{
try
{
// Load the XML file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
// Read the XML data
XmlNodeList nodes = xmlDoc.SelectNodes("//book");
foreach (XmlNode node in nodes)
{
string title = node.SelectSingleNode("title").InnerText;
string author = node.SelectSingleNode("author").InnerText;
string price = node.SelectSingleNode("price").InnerText;
lstXmlData.Items.Add(string.Format("Title: {0}\tAuthor: {1}\tPrice: {2}", title, author, price));
}
}
catch (XmlException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
MessageBox.Show("Please enter XML file path.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}
}
```
在此示例中,我们在窗口中添加了一个文本框和一个按钮,用于输入XML文件的路径并读取XML数据。我们还添加了一个列表框,用于显示读取的XML数据。
在“Read XML”按钮的单击事件处理程序中,我们首先清除列表框中的所有项。然后,我们从文本框中获取XML文件的路径,并使用XmlDocument类加载XML文件。接下来,我们选择所有的“book”节点,并使用SelectSingleNode方法读取每个节点的“title”、“author”和“price”元素的InnerText属性。最后,我们将读取的数据添加到列表框中。
如果加载XML文件时发生异常(例如,文件不存在或格式不正确),我们将通过MessageBox.Show方法显示错误消息。
你现在是c#专家,请帮我用c#语言写一个wpf程序,它需要做到点击按键1,选择读取某一个文件夹下所有dat文件,并在textbox或text block或label中显示文件夹路径。点击按键2,实现该文件夹下所有dat文件的如下转换功能,读取每一行数据,这一行的每一列数据的分隔符从之前的多个空格或逗号或多个tab,全部替换为一个Tab。替换之后覆盖保存原文件。
好的,下面是用C#语言编写的WPF程序的示例代码,可以实现您的需求:
首先,在XAML文件中添加如下布局:
```xml
<Window x:Class="WpfApp1.MainWindow"
...
Title="文件转换工具" Height="250" Width="350">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="文件夹路径:" Margin="10" Grid.Row="0"/>
<TextBox x:Name="txtFolderPath" Margin="10" Grid.Row="1"/>
<Button Content="选择文件夹" Margin="10" Grid.Row="2" Click="Button_Click"/>
<Button Content="转换文件" Margin="10" Grid.Row="3" Click="Button_Click_1"/>
</Grid>
</Window>
```
接着,在MainWindow.xaml.cs文件中添加代码实现按键1和按键2的功能:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Win32;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// 弹出文件夹选择对话框
var dlg = new OpenFileDialog();
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
dlg.Filter = "DAT Files (*.dat)|*.dat";
dlg.Multiselect = false;
dlg.ShowDialog();
// 显示选择的文件夹路径
txtFolderPath.Text = Path.GetDirectoryName(dlg.FileName);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// 获取文件夹路径
var folderPath = txtFolderPath.Text;
// 遍历文件夹下的所有DAT文件
foreach (var filePath in Directory.EnumerateFiles(folderPath, "*.dat"))
{
// 读取文件内容
var lines = File.ReadAllLines(filePath);
// 转换每一行的数据
var newLines = new List<string>();
foreach (var line in lines)
{
var columns = line.Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
var newLine = string.Join("\t", columns);
newLines.Add(newLine);
}
// 覆盖保存原文件
File.WriteAllLines(filePath, newLines, Encoding.UTF8);
}
// 显示转换完成的消息框
MessageBox.Show("文件转换完成!");
}
}
}
```
这样,当用户点击按键1时,会弹出一个文件夹选择对话框,用户选择完成后会将选择的文件夹路径显示在文本框中;当用户点击按键2时,会遍历文件夹下的所有DAT文件,读取每一行数据并进行转换,最后覆盖保存原文件,完成文件转换。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)