LINQ读取文件XML

时间:2016-06-17 09:52:16

标签: c# xml linq

我有这个文件XML:

this is my XML file

如何使用LINQ获取C#代码? 我试试这段代码:

Sub setAdEndDate(monvidPath)
    Const sXPath  = "/XMLFeederRoot/Ad/End_Date"

    Set xmlDoc = CreateObject("Microsoft.XMLDOM")

    xmlDoc.Async = "False"
    xmlDoc.Load(monvidPath)

    Set colNodes=xmlDoc.selectNodes(sXPath)

    For Each n In colNodes

        n.Text = SplitArray(n.Text)

    Next

    xmlDoc.Save monvidPath

End Sub

Function SplitArray(strval1)
    Dim SplitArray1, SplitArray2, strSDat

    splitArray1 = Split(strval1, "-")
    splitArray1(2) = Left(splitArray1(2), 2)
    strDate1 = SplitArray1(1) & "/" & SplitArray1(2) & "/" & SplitArray1(0)
    strDate30 = DateAdd("d", 30, strDate1)

    SplitArray2 = Split(strDate30, "/")

    'Add zero to the left
    If Len(SplitArray2(0))<2 Then 
        SplitArray2(0)= Right("0" & SplitArray2(0), 2)
    End If
    If Len(SplitArray2(1))<2 Then 
        SplitArray2(1)= Right("0" & SplitArray2(1), 2)
    End If

    SplitArray2(1) = splitArray2(1) & "T00:00:00"
    strSDat = SplitArray2(2) & "-" & SplitArray2(0) & "-" & SplitArray2(1)
    SplitArray= strSDat

End Function

但我没有结果!

2 个答案:

答案 0 :(得分:0)

问题是,你的孩子根本没有属性,只有价值观,不是吗?

如果你像这样改变你的周期怎么样,这是你想要的吗?

foreach (var lv1 in lv1s)
{
    result.AppendLine(lv1.Header);
    foreach (var lv2 in lv1.Children)
    {
        //result.AppendLine("     " + lv2.Attribute("type").Value); // THERE ARE NO ATTRIBUTES
        result.AppendLine("     " + lv2.Value);
    }
}

答案 1 :(得分:0)

非常感谢,你给我的代码,我还有问题..

但我用其他方法找到了解决方案:

        XmlDocument doc = new XmlDocument();
        doc.Load(@"C:\temp\image.xml");


        XmlNodeList elemList = doc.GetElementsByTagName("value");
        for (int i = 0; i < elemList.Count; i++)
        {
            Console.WriteLine(elemList[i].InnerXml);
        }

如果我想要指定一个值,例如,对于例子类型=“line1”:

string aa = elemList[1].InnerXml;