C# 数组转换

发布于 2024-09-28 19:56:50 字数 923 浏览 3 评论 0原文

任何帮助,因为我是 C# 菜鸟。以下代码工作正常并返回 1 个字符串 ViewState2。我希望它返回一个 ViewState2 和 EventValidation2 数组,以便我稍后可以对其进行操作。我如何将下面的代码转换为返回数组?

    public string get_status(string local_fname)
    {
        var dts_doc = new HtmlAgilityPack.HtmlDocument();
        dts_doc.Load(local_fname);

        //Pull the values
        var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]");
        var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]");

        string ViewState2 = ViewState.Attributes[3].Value;
        string EventValidation2 = EventValidation.Attributes[3].Value;


        //Display the values

        //System.Console.WriteLine(ViewState.Attributes[3].Value);
        //System.Console.WriteLine(EventValidation.Attributes[3].Value);
        //System.Console.ReadKey();
        return ViewState2;
    }

Any help here as I'm a C# noob. The following code works fine and returns 1 string ViewState2. I'd like it to return an array of ViewState2 and EventValidation2 so I can manipulate it later on. How would I convert the code below to return an array?

    public string get_status(string local_fname)
    {
        var dts_doc = new HtmlAgilityPack.HtmlDocument();
        dts_doc.Load(local_fname);

        //Pull the values
        var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]");
        var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]");

        string ViewState2 = ViewState.Attributes[3].Value;
        string EventValidation2 = EventValidation.Attributes[3].Value;


        //Display the values

        //System.Console.WriteLine(ViewState.Attributes[3].Value);
        //System.Console.WriteLine(EventValidation.Attributes[3].Value);
        //System.Console.ReadKey();
        return ViewState2;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人哭 2024-10-05 19:56:50

不要使用数组,而使用类。这样做,您不必记住每个元素的含义。

public class Status
{
  public string ViewState {get; set;}
  public string EventValidation {get; set;}
}

using System;
using HtmlAgilityPack;

[...]

public Status GetStatus(string localFileName)
{
    var dtsDoc = new HtmlDocument();
    dtsDoc.Load(localFileName);

    //Pull the values
    var viewStateNode = dtsDoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]");
    var eventValidationNode = dtsDoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]");

    string viewState = viewStateNode.Attributes[3].Value;
    string eventValidation = eventValidationNode.Attributes[3].Value;


    //Display the values

    //Console.WriteLine(viewState);
    //Console.WriteLine(eventValidation);
    //Console.ReadKey();
    return new Status
    {
      ViewState = viewState,
      EventValidation = eventValidation
    }
}

另外,您应该阅读 C# 语言中的编码指南和命名约定,using 语句也可能很有趣。我纠正了一些“错误”,但可能没有抓住全部。此外,我还重命名了几个变量,以使它们的内容更清晰。您可能还想考虑仅在循环中使用 var 关键字,同时使用 LINQ(或一般的匿名类型)或使用非常长的类名。写出类型名称可以大大提高可读性。

Don't use an array, but a class. Doing this, you don't have to remember what each element means.

public class Status
{
  public string ViewState {get; set;}
  public string EventValidation {get; set;}
}

using System;
using HtmlAgilityPack;

[...]

public Status GetStatus(string localFileName)
{
    var dtsDoc = new HtmlDocument();
    dtsDoc.Load(localFileName);

    //Pull the values
    var viewStateNode = dtsDoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]");
    var eventValidationNode = dtsDoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]");

    string viewState = viewStateNode.Attributes[3].Value;
    string eventValidation = eventValidationNode.Attributes[3].Value;


    //Display the values

    //Console.WriteLine(viewState);
    //Console.WriteLine(eventValidation);
    //Console.ReadKey();
    return new Status
    {
      ViewState = viewState,
      EventValidation = eventValidation
    }
}

Also, you should read up on coding guidelines and naming conventions in the C# language, also the using statement might be interesting. I have corrected some "mistakes", but probably didn't catch all. Also, I have renamed a couple of variables, to make their content clearer. You also might want to look into using the var keyword only in a loop, while using LINQ (or anomynous types in general) or with really long class names. Written out type names can increase readability quite a lot.

断爱 2024-10-05 19:56:50

如果您确实想要一个包含 ViewState2EventValidation2 的数组,您可以进行以下更改:

// Notice: return value of string[] instead of string
public string[] get_status(string local_frame);

并且:

// Notice: returning an array
return new string[] { ViewState2, EventValidation2 };

也就是说,这确实是“快速而肮脏”的方法,如果您希望此代码可维护,则不太合适(您最后一次阅读有关“返回长度为 2 的数组,其中一个字符串表示 X 作为第一个元素,另一个字符串表示Y 作为第二个”?)。

Femaref 是对的正确的做法是将您想要返回的信息封装在它自己的类型中。

If you really want an array with ViewState2 and EventValidation2 in it, you can make the following changes:

// Notice: return value of string[] instead of string
public string[] get_status(string local_frame);

And:

// Notice: returning an array
return new string[] { ViewState2, EventValidation2 };

That said, this is really the "quick and dirty" approach, and is not really appropriate if you're going to want this code to be maintainable (when's the last time you read documentation on a function that "returns an array of length 2, with a string representing X as the first element and another string representing Y as the second"?).

Femaref's right; the correct thing to do would be to encapsulate the information you want returned in its own type.

灯角 2024-10-05 19:56:50

假设您对这个问题的回答是“是”(尽管我建议采用不同的方法,请参见下文),这将满足您的要求:

public String[] get_status(string local_fname) 
{ 
    var dts_doc = new HtmlAgilityPack.HtmlDocument(); 
    dts_doc.Load(local_fname); 

    //Pull the values 
    var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]"); 
    var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]"); 

    string ViewState2 = ViewState.Attributes[3].Value; 
    string EventValidation2 = EventValidation.Attributes[3].Value; 

    String[] retValues = new String[2];
    retValues[0] = ViewState2;
    retValues[1] = EventValidation2;

    return retValues;

    //Display the values 

    //System.Console.WriteLine(ViewState.Attributes[3].Value); 
    //System.Console.WriteLine(EventValidation.Attributes[3].Value); 
    //System.Console.ReadKey(); 
    return ViewState2; 
} 

也就是说,我会遵循该方法。


我会编写一个包含您想要的数据成员的类:

public class DataClass
{
    public string ViewState { get; set; }
    public string EventValidation { get; set; }
}

然后我会修改该方法以返回数据类的实例。

public DataClass get_status(string local_fname) 
{ 
    var dts_doc = new HtmlAgilityPack.HtmlDocument(); 
    dts_doc.Load(local_fname); 

    //Pull the values 
    var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]"); 
    var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]"); 

    var dc = new DataClass();

    dc.ViewState = ViewState.Attributes[3].Value;
    dc.EventValidation = EventValidation.Attributes[3].Value;

    return dc;
} 

Assuming you answer yes to this question (although I'd recommend a different approach, see below) this will do what you're asking:

public String[] get_status(string local_fname) 
{ 
    var dts_doc = new HtmlAgilityPack.HtmlDocument(); 
    dts_doc.Load(local_fname); 

    //Pull the values 
    var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]"); 
    var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]"); 

    string ViewState2 = ViewState.Attributes[3].Value; 
    string EventValidation2 = EventValidation.Attributes[3].Value; 

    String[] retValues = new String[2];
    retValues[0] = ViewState2;
    retValues[1] = EventValidation2;

    return retValues;

    //Display the values 

    //System.Console.WriteLine(ViewState.Attributes[3].Value); 
    //System.Console.WriteLine(EventValidation.Attributes[3].Value); 
    //System.Console.ReadKey(); 
    return ViewState2; 
} 

That said, I would follow the approach afte the line.


I'd write a class that has the data members you want:

public class DataClass
{
    public string ViewState { get; set; }
    public string EventValidation { get; set; }
}

Then I'd modify the method to return an instance of your data class.

public DataClass get_status(string local_fname) 
{ 
    var dts_doc = new HtmlAgilityPack.HtmlDocument(); 
    dts_doc.Load(local_fname); 

    //Pull the values 
    var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]"); 
    var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]"); 

    var dc = new DataClass();

    dc.ViewState = ViewState.Attributes[3].Value;
    dc.EventValidation = EventValidation.Attributes[3].Value;

    return dc;
} 
甜宝宝 2024-10-05 19:56:50
string[] array = new string[2];

array[0] = ViewState2;
array[1] = EventValidation2;

return array;

但这似乎是微不足道的答案。请问它能解决您的问题吗?如果不是,您能更好地说明问题吗?

string[] array = new string[2];

array[0] = ViewState2;
array[1] = EventValidation2;

return array;

But it seems to trivial as answer. Please Does it solve your problem? If no, can you specify better the question please?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文