将VB Linq转换为XML到C# [英] converting VB Linq to XML to C#

查看:106
本文介绍了将VB Linq转换为XML到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将所有应用程序从VB.Net转换为C#.

在此问题的第一个版本中,我为简洁起见而努力,但可能太简短了.所以我会再试一次.

以我的有限经验,VB和C#中的LINQ语法相似.例如,在VB中,我可以编写

I''m in the process of converting all my applications from VB.Net to C#.

In the first version of this question I was striving for brevity but may have been too brief. So I''ll try again.

In my limited experience, LINQ syntax in VB and C# is similar. For example, in VB I can write

Dim myArrayOfStateNames(3) As String

  Sub FillStates()
      myArrayOfStateNames(0) = "WA"
      myArrayOfStateNames(1) = "OR"
      myArrayOfStateNames(2) = "ID"
      myArrayOfStateNames(3) = "CA"
  End Sub

  ReadOnly Property GetIdaho() As String
      Get
          Dim myState = From oneState In myArrayOfStateNames Where oneState = "ID" Select oneState
          Return myState.First
      End Get
  End Property


同样,在C#中,我可以编写


Similarly in C# I can write

string[] myArrayOfStateNames = new string[4];
  public void FillStates()
 {
     myArrayOfStateNames[0] = "WA";
     myArrayOfStateNames[1] = "OR";
     myArrayOfStateNames[2] = "ID";
     myArrayOfStateNames[3] = "CA";
 }
 public string GetIdaho
 {
     get
     {
         var myState = from oneState in myArrayOfStateNames where oneState == "ID" select oneState;
         return myState.First();
     }



在这些示例中,"myState ="之后的两个LINQ语句对我而言非常相似.语法略有不同,但是很接近.

但是,当我尝试将LINQ与XML结合使用时,事情显然不是那么简单.在VB中,我可以写



In these examples, the two LINQ statements after ''myState = '' are, to me anyway, very similar. The syntax is a little different, but close.

However, when I try to use LINQ with XML, things are apparently not so straightforward. In VB I can write

Dim myXML As New System.Xml.Linq.XDocument
 Sub MakeSomeXML()
     myXML = _
  <?xml version="1.0" encoding="utf-8"?>
  <?qbxml version="7.0"?>
  <QBXML>
      <QBXMLMsgsRq onError="stopOnError">
          <ItemQueryRq>
              <FullName>Joe Jones</FullName>
              <OwnerID>0</OwnerID>
          </ItemQueryRq>
      </QBXMLMsgsRq>
  </QBXML>
 End Sub
 ReadOnly Property XMLName() As String
     Get
         Dim name = From someXML In myXML...<ItemQueryRq> Select someXML.<FullName>
         Return name.First.Value
     End Get
 End Property



但是当我尝试像这样转换为C#时



But when I try to convert to C# like this

System.Xml.Linq.XDocument myXML;
        
        public void MakeSomeXML()
        {
            System.Xml.Linq.XDocument myXML = System.Xml.Linq.XDocument.Parse(
            @" <?xml version=""1.0"" encoding=""utf-8""?>
                <?qbxml version=""7.0""?>
                    <QBXML>
                        <QBXMLMsgsRq onError=""stopOnError"">
                        <ItemQueryRq>
                            <FullName>Joe Jones</FullName>
                            <OwnerID>0</OwnerID>
                        </ItemQueryRq>
                    </QBXMLMsgsRq>
                </QBXML>");
        }
        public string XMLName
        {
            get
            {
                string name = from someXML in myXML...<ItemQueryRq> select someXML.<FullName>;
                              return name;
            }
        }



来自myXML中的someXML"之后,C#在linq语法上严重窒息,并且C#中的intellisense不会像在VB中那样显示"...<>".语法似乎根本不相似.

有两个问题:
1)我是否缺少明显的东西?
2)如果不是,在C#中解决此问题的正确/最佳方法是什么? C#示例中的LINQ语法显然是错误的,它是可纠正的还是我走在一条完全错误的道路上?我是否应该使用具有不同语法的"from myXML中的from someXML"类型的语句buth?改用XMLReader?使用XPath吗?

非常感谢.



C# chokes badly on the linq syntax after ''from someXML in myXML'' and the intellisense in C# does not show ''...<>'' as it does in VB. the syntax is seemingly not at all similar.

So two questions:
1) am I missing something obvious?
2) if not, what is the proper/best way to approach this in C#? The LINQ syntax in the C# example is obviously wrong, is it correctable or am I on a totally wrong path? Should I use a ''from someXML in myXML'' type of statement buth with a different syntax? Use XMLReader instead? Use XPath?

Thanks much.

推荐答案

您只张贴了原始的VB代码,也许您可​​以添加自己编写的C#代码,然后有人可以为您提供帮助.
You have onle posted the original VB code, perhaps you could add the C# code that you have written and then someone may be able to help you.


为回答一个答案(并感谢您的回答),我进行了详细说明并重写了问题.
In response to the one answer (and thanks for the answer) I went for verbosity and rewrote the question.


Dell.Simmons写道:
Dell.Simmons wrote:

字符串名称=来自myXML中的someXML ...< itemqueryrq>选择someXML.<全名> ;;

string name = from someXML in myXML...<itemqueryrq> select someXML.<fullname>;



我不认为这是C#,缺乏智能通常是一个线索.但是,由于不熟悉VB,我不知道应该怎么做.



I don''t think that this is C#, the lack of intellisense is usually a clue. However, I have no idea what it should be as I''m not that familiar with VB.


这篇关于将VB Linq转换为XML到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆