我试图通过从数据集中提取数据来输出XML文件.但是,xml文件始终为空.你能帮我找一下代码中的错误吗?
class Program
{
static void Main(string[] args)
{
string umail = "";
XDocument loaded = XDocument.Load(@"C:\1.xml");
var q = from c in loaded.Descendants("AdminUserDB.dbo.U_User")
select (string)c.Element("URI");
foreach (string em in q)
umail = em;
SqlConnection cn = new SqlConnection("server=(local);database=AdminUserDB;Persist Security Info=True; uid=sa;pwd=P@swrd123");
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM dbo.U_User WHERE URI=@umail", cn);
da.SelectCommand.Parameters.AddWithValue("@umail", umail);
da.Fill(ds);
string filename = "output.xml";
System.IO.FileStream myFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
ds.WriteXml(myXmlWriter);
myXmlWriter.Close(); …Run Code Online (Sandbox Code Playgroud) 我有一个计时器,每秒循环通过以下代码.这段代码一个接一个地读取几个xml片段,然后将它们打印到屏幕上,然后在一秒钟之后再重复一遍.输出:
34,
23,
12,
Run Code Online (Sandbox Code Playgroud)
我想要做的是将所有数据写入一个字符串,如34,23,12,然后返回它,以便另一个类可以使用它.但是,如果我把它(字符串代码,你会在下面看到)在if (fragmentReader.Read())代码下它不起作用,因为它只是与它上面的console.writeline做同样的事情:读取其中一个片段然后写入控制台,然后是下一个片段,它不会将片段中的所有值都添加到一个字符串中.
谢谢
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
{
using (var readerz = file.CreateViewAccessor(0, 0))
{
var bytes = new byte[195];
var encoding = Encoding.ASCII;
readerz.ReadArray<byte>(0, bytes, 0, bytes.Length);
//File.WriteAllText("C:\\myFile.txt", encoding.GetString(bytes));
StringReader stringz = new StringReader(encoding.GetString(bytes));
var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var reader = XmlReader.Create(stringz, readerSettings))
{
while (reader.Read())
{
using (var fragmentReader = reader.ReadSubtree())
{
if (fragmentReader.Read())
{
reader.ReadToFollowing("value"); …Run Code Online (Sandbox Code Playgroud) 我有以下代码.一个switch语句,它接受一个州名并将另一个变量设置为该状态的abbriviation.但是,在我关闭我的switch语句后,我得到一个错误,说变量无法访问.
string stateab;
switch (s)
{
case "Alabama":
stateab = "AL";
break;
case "Alaska":
stateab = "AK";
break;
case "Arizona":
stateab = "AZ";
break;
case "Arkansas":
stateab = "AR";
break;
case "California":
stateab = "CA";
break;
case "Colorado":
stateab = "CO";
break;
case "Connecticut":
stateab = "CT";
break;
case "Delaware":
stateab = "DE";
break;
case "DENIED":
stateab = "DENIED";
break;
case "District of Columbia":
stateab = "DC";
break;
case "Florida":
stateab = "FL";
break;
case "Georgia":
stateab = "GA";
break; …Run Code Online (Sandbox Code Playgroud) 我已经尝试了几种方法来做到这一点...基本上我正在尝试为一个任务创建一个tic tac toe board,也许我错过了一些明显的东西,但当我发现时,我得到一个"非声明"错误尝试创建按钮.这是我得到的代码:
int rows = 3;
int cols = 3;
JPanel ticTacToeBoard = new JPanel();
ticTacToeBoard.setLayout(new GridLayout(3, 3));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
JButton gameButton[i] = new JButton[];
ticTacToeBoard.add(gameButton[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢...
我没有太多的代码要显示,但我试图从一个较长的字符串中删除子字符串(单个数字)是一系列数字.
例如:主字符串是"11,12,15,16,55,33,88,100,121,155,115"
需要找到16号并将其从字符串中删除...
11,12,15,55,33,88,100,121,155,115
它们是数据库中id的列表,因此我不能将它们更改为字符串.另外如何将其删除,就好像它不存在一样?
我决定学习C,这是我使用的其中一本书的片段:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t result = fork();
if (result == -1){
fprintf(stderr, "Error\n");
return 1;
}
if (result == 0)
printf("I'm a child with PID = %d\n", getpid());
else
printf("I'm a parent with PID = %d\n", getpid());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的输出是:
I'm a parent with PID = 5228
I'm a child with PID = 5229
Run Code Online (Sandbox Code Playgroud)
一切都清楚,但怎么可能是result == 0和result != 0在同一时间?看起来这个变量存储了两个值,因为printf指令执行了两次.我知道,那会fork()返回0父亲的PID,但是如何result检查它是否在不同条件下返回true?
public override bool Equals(object obj)
{
var c = obj as myObj;
if (c == null) return false;
return this.Id.Equals(c.Id)
&& this.Email.Equals(c.Email)
&& this.code.Equals(c.code)
&& (this.myVal == null) ? true : (this.myVal.Equals(c.myVal))
}
Run Code Online (Sandbox Code Playgroud)
如图所示的这个方法应该返回一个布尔值.当"this.myVal"中的值为null时,我只想返回true(我正在上面做).相反,我得到一个"对象引用未设置为对象的实例"在检查this.myVal时导致.此错误表明C#编译器不关心我的"null"检查?为什么要抱怨空引用?