XML string to XML document

后端 未结 4 718
夕颜
夕颜 2021-01-30 10:36

I have a whole XML document in a String which i need to convert to a XML document and parse tags in the document

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 10:43

    Using Linq to xml

    Add a reference to System.Xml.Linq

    and use

    XDocument.Parse(string xmlString)
    

    Edit: Sample follows, xml data (TestConfig.xml)..

    
    
      
        Convert number to string
        Examp1.EXE
        1
        One
      
      
        Find succeeding characters
        Examp2.EXE
        abc
        def
      
      
        Convert multiple numbers to strings
        Examp2.EXE /Verbose
        123
        One Two Three
      
      
        Find correlated key
        Examp3.EXE
        a1
        b1
      
      
        Count characters
        FinalExamp.EXE
        This is a test
        14
      
      
        Another Test
        Examp2.EXE
        Test Input
        10
      
    
    

    C# usage...

    XElement root = XElement.Load("TestConfig.xml");
    IEnumerable tests =
        from el in root.Elements("Test")
        where (string)el.Element("CommandLine") == "Examp2.EXE"
        select el;
    foreach (XElement el in tests)
        Console.WriteLine((string)el.Attribute("TestId"));
    

    This code produces the following output: 0002 0006

提交回复
热议问题