我有一个枚举
string name;
public enum Color
{
Red,
Green,
Yellow
}
Run Code Online (Sandbox Code Playgroud)
如何在加载时将其设置为NULL.
name = "";
Color color = null; //error
Run Code Online (Sandbox Code Playgroud)
编辑:我的不好,我没有正确解释.但所有与可空的相关的答案都是完美的.我的情况是如果,我已经在类中使用其他元素(例如名称等)获取/设置枚举.在页面加载时,我初始化类并尝试将值默认为null.这是场景(代码在C#中):
namespace Testing
{
public enum ValidColors
{
Red,
Green,
Yellow
}
public class EnumTest
{
private string name;
private ValidColors myColor;
public string Name
{
get { return name; }
set { name = value; }
}
public ValidColors MyColor
{
get { return myColor; }
set { myColor = value; }
}
}
public partial class _Default : …Run Code Online (Sandbox Code Playgroud) 我有类似的要求:将HH:MM:SS格式的时间转换为仅秒?
但在javascript中.我已经看到很多将秒转换成不同格式的例子,但不是HH:MM:SS转换为秒.任何帮助,将不胜感激.
private static string WebServiceCall(string methodName)
{
WebRequest webRequest = WebRequest.Create("http://localhost/AccountSvc/DataInquiry.asmx");
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.Headers.Add("SOAPAction: http://tempuri.org/" + methodName);
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
Stream requestStream = httpRequest.GetRequestStream();
//Create Stream and Complete Request
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
soapRequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
soapRequest.Append("</soap:Body></soap:Envelope>");
streamWriter.Write(soapRequest.ToString());
streamWriter.Close();
//Get the Response
HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
StreamReader srd = new StreamReader(wr.GetResponseStream());
string resulXmlFromWebService = srd.ReadToEnd();
return resulXmlFromWebService;
} …Run Code Online (Sandbox Code Playgroud) 有关XDocuments和Linq的新手,请建议一个从xml字符串中的特定标记中检索数据的解决方案:
如果我有一个来自webservice响应的XML字符串(为了方便我格式化了xml):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCashFlowReportResponse xmlns="http://tempuri.org/">
<GetCashFlowReportPdf>Hello!</GetCashFlowReportPdf>
</GetCashFlowReportResponse>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)
使用以下代码,仅当GetCashFlowReportResponsetag没有"xmlns"属性时才能获取值.不知道为什么?否则,它始终返回null.
string inputString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><GetCashFlowReportResponse xmlns=\"http://tempuri.org/\"><GetCashFlowReportPdf>Hello!</GetCashFlowReportPdf></GetCashFlowReportResponse></soap:Body></soap:Envelope>"
XDocument xDoc = XDocument.Parse(inputString);
//XNamespace ns = "http://tempuri.org/";
XNamespace ns = XNamespace.Get("http://tempuri.org/");
var data = from c in xDoc.Descendants(ns + "GetCashFlowReportResponse")
select (string)c.Element("GetCashFlowReportPdf");
foreach (string val in data)
{
Console.WriteLine(val);
}
Run Code Online (Sandbox Code Playgroud)
我无法更改Web服务以删除该属性.是否有更好的方法来读取响应并将实际数据返回给用户(以更易读的形式)?
编辑: 解决方案:
XDocument xDoc = XDocument.Parse(inputString);
XNamespace ns = "http://tempuri.org/";
var data = from c in xDoc.Descendants(ns …Run Code Online (Sandbox Code Playgroud) 我们在SQL Server中有TestPartner数据库.错误的描述存储在"image"数据类型列中.我们需要编写一个查询来将数据显示为html表.我们有这个查询从相应的表中读取数据以使用xml显示信息For XML.但是将image数据类型转换为varchar会引发异常:"FOR XML无法序列化节点'TD'的数据,因为它包含XML中不允许的字符(0x0002).要使用FOR XML检索此数据,请将其转换为二进制,varbinary或image数据类型,并使用BINARY BASE64指令."
查询:
DECLARE @ResultsTable nvarchar(MAX)
--Create the XML table with the query results
SET @ResultsTable =
N'<H3>QA Automation Tests Results Summary </H3>' +
N'<table border="1">' +
N'<tr><th>Test Name</th><th>Execution Date</th>' +
N'<th>Check Name</th><th>Description</th></tr>' +
CAST ( (
select distinct Name as TD, '',
(Select CAST(CONVERT(nchar(100),CAST( TPCommandDetail AS BINARY(100) )) as VARCHAR(100)) ) as TD, ''
FROM TestPartnerDB.TP_RESULTS_RECORDS
FOR XML PATH('tr'), TYPE
) AS nvarchar(max) ) + N'</table>'
SELECT @ResultsTable
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,它适用于某些记录,并且只要你将大小设置为200即可.它会再次抛出错误.我也尝试过:
Select CONVERT(varchar(1000), convert(varbinary(1000), tpcommanddetail)) …Run Code Online (Sandbox Code Playgroud) 如何使用c#中的反射读取包含数组类型元素的对象的属性.如果我有一个名为GetMyProperties的方法,并且我确定该对象是自定义类型,那么我该如何读取数组的属性及其中的值.IsCustomType是确定类型是否为自定义类型的方法.
public void GetMyProperties(object obj)
{
foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
{
if (!Helper.IsCustomType(pinfo.PropertyType))
{
string s = pinfo.GetValue(obj, null).ToString();
propArray.Add(s);
}
else
{
object o = pinfo.GetValue(obj, null);
GetMyProperties(o);
}
}
}
Run Code Online (Sandbox Code Playgroud)
场景是,我有一个ArrayClass和ArrayClass的对象有两个属性:
-string Id
-DeptArray[] depts
Run Code Online (Sandbox Code Playgroud)
DeptArray是另一个具有2个属性的类:
-string code
-string value
Run Code Online (Sandbox Code Playgroud)
因此,此方法获取ArrayClass的对象.我想在字典/列表项中读取所有属性从上到下并存储名称/值对.我能够为价值,定制,枚举类型做到这一点.我被一堆物体困住了.不知道怎么做.
我有一个现有的StringBuilder对象,代码附加一些值和一个分隔符.现在我想修改代码以添加逻辑,在附加我要检查的文本之前是否确实存在于字符串生成器变量中?如果没有,那么只有附加否则忽略.这样做的最佳方法是什么?我是否需要将对象更改为字符串类型?需要一种不会妨碍性能的最佳方法.
public static string BuildUniqueIDList(context RequestContext)
{
string rtnvalue = string.Empty;
try
{
StringBuilder strUIDList = new StringBuilder(100);
for (int iCntr = 0; iCntr < RequestContext.accounts.Length; iCntr++)
{
if (iCntr > 0)
{
strUIDList.Append(",");
}
//need to do somthing like strUIDList.Contains(RequestContext.accounts[iCntr].uniqueid) then continue other wise append
strUIDList.Append(RequestContext.accounts[iCntr].uniqueid);
}
rtnvalue = strUIDList.ToString();
}
catch (Exception e)
{
throw;
}
return rtnvalue;
}
Run Code Online (Sandbox Code Playgroud)
我不确定是否有类似的东西会有效:if(!strUIDList.ToString().Contains(RequestContext.accounts [iCntr] .uniqueid.ToString()))
我有一个XML
<data>
<summary>
<account curr_desc='USD' acct_nbr='123' net='1000.00' />
<account curr_desc='USD' acct_nbr='456' net='2000.00' />
</summary>
<details>
<accounts>
<account acct_nbr="123" curr="USD">
<activity color='False' settle_date='02 Jul 2010' amt='580.00' />
<activity color='True' settle_date='09 Jul 2010' amt='420.00' />
</account>
<account acct_nbr="456" curr="USD">
<activity color='True' settle_date='12 Dec 2010' amt='1500.00' />
<activity color='True' settle_date='19 Dec 2010' amt='500.00' />
</account>
</accounts>
</details>
</data>
Run Code Online (Sandbox Code Playgroud)
使用Linq和XDocument,我可以提取"摘要"信息,但如何在"摘要"标签下提取"帐户"信息?
XDocument XMLDoc = XDocument.Load("testdata.xml");
XElement accounts = (from xml2 in XMLDoc.Descendants("summary")
select xml2).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
如何指定像" summary/account"这样的东西,以便它返回下面的所有元素<summary>?请注意,我有<account>下<detail><accounts>,我只希望在总结标签的元素.
编辑:(安德鲁的解决方案)
<data> …Run Code Online (Sandbox Code Playgroud) 我有一个解决方案WCFSampleSolution,它有我的所有项目 - Web服务,客户端和网站.结构类似于:
我为我的服务创建了WCFService项目.它包含IService1.cs和Service1.cs.然后我在IIS中托管了该服务.我这样做是通过创建一个网站并将.svc和web.config文件添加到网站项目中.然后在IIS中发布它.当我运行http:\ MyMachineName\Website\Service.svc时,它会显示服务描述.然后我创建调用webservice的Web客户端.我使用服务引用来添加服务.它调用Service1的方法.它工作正常.但我无法调试此程序/设置.我验证了WCFWebClient项目和网站项目中的配置文件,他们有适当的调试设置.
<compilation debug="true">
Run Code Online (Sandbox Code Playgroud)
我把断点但控制从来没有进入我的seb服务.我也试过附加过程,但它也行不通.但我能够调试其他一个WCF项目.设置略有不同.在那个项目中,我在我的Web客户端中复制了.svc文件和配置,调试工作正常.
请帮忙!!
我有一些存储在DataTable中的数字代码.当我尝试使用DataView对其进行排序时,它按字符串对列进行排序.将数据排序为整数/数字的最简单方法是什么?
DataView view = dt.DefaultView();
view.Sort = "Code asc";
dt = view.ToTable();
Run Code Online (Sandbox Code Playgroud)
数据表中的数据:128,123,112,12,126
排序后显示:112,12,123,126,128
预期结果:12,112,123,126,128
c# ×7
linq-to-xml ×2
web-services ×2
xml ×2
.net ×1
arrays ×1
asmx ×1
contains ×1
debugging ×1
enums ×1
javascript ×1
null ×1
nullable ×1
object ×1
propertyinfo ×1
reflection ×1
soap ×1
sorting ×1
sql ×1
string ×1
time ×1
wcf ×1