Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

How to convert List<string> to xml document in c#

I would like to convert the List to a xml document in c#.

My code is as follows:

List<string> BugWSResponseList1 = new List<string>();
Logger.Write("

" + DateTime.Now + " : " + " : START : Creation of a set of Bugs via bug.Add API");
BugWSResponseList1 = CreateBugs(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
Logger.Write("

" + DateTime.Now + " : " + " : END : Creation of a set of Bugs via bug.Add API");

I have been trying to convert the list response to string and then tried to convert it in xml document but when i parse it then it shows that there are more than one root elements in the xml.

The format of xml is as follows:

<Bug>
      <family>ESG</family>
      <product>Dr.Watson</product>
      <version>Xpress API</version>
      <productarea>1</productarea>
      <subarea></subarea>
      <qe>sdawar</qe>
      <duplicateId></duplicateId>
</Bug>
<Bug>
      <family>TEST22</family>
      <product>Dr.Watson</product>
      <version>Xpress API</version>
      <productarea>1</productarea>
      <subarea></subarea>
      <qe>sdawar</qe>
      <duplicateId></duplicateId>
</Bug>

Please note that it has two different nodes in the xml. What i am doing so far is converting List BugWSResponseList1 to a string and then loading it as xml.here is the code i am doing:

XmlDocument xd = new XmlDocument();
                    string s = string.Join(",", BugWSResponseList2);                    
                    xd.LoadXml(s);

But when i am doing it it says that there are more than one root element in the xml.

I would like to convert the List BugWSResponseList1 to an xml document as i need to parse it further for my code execution. Any help would be appreciated. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may use XElement or XDocument and LINQ. Providing a sample of what the XML should look like we could provide more info.

For instance:

BugWSResponseList1.Add("<Bug><family>TEST22</family><product>Dr.Watson</product><version>Xpress API</version><productarea>1</productarea><subarea></subarea><qe>sdawar</qe><duplicateId></duplicateId></Bug>");

BugWSResponseList1.Add("<Bug><family>ESG</family><product>Dr.Watson</product><version>Xpress API</version><productarea>1</productarea><subarea></subarea><qe>sdawar</qe><duplicateId></duplicateId></Bug>");

XElement xe = new XElement
              (
                  "Bugs",
                  BugWSResponseList1
                  .Select 
                  (
                      x=>
                      XElement.Parse(x)
                  )
              );

So after i have loaded the list with the two pieces of xml you have provided i do the following:

I create a new XElement that will hold the root of the XML. I name that root as Bugs. Then as children of that root i put the contents of your List after parsing them into XElement objects.

The above code will result to :

<Bugs>
  <Bug>
    <family>TEST22</family>
    <product>Dr.Watson</product>
    <version>Xpress API</version>
    <productarea>1</productarea>
    <subarea></subarea>
    <qe>sdawar</qe>
    <duplicateId></duplicateId>
  </Bug>
  <Bug>
    <family>ESG</family>
    <product>Dr.Watson</product>
    <version>Xpress API</version>
    <productarea>1</productarea>
    <subarea></subarea>
    <qe>sdawar</qe>
    <duplicateId></duplicateId>
  </Bug>
</Bugs>

What you were missing was the root element. XmlDocument needs to have a root element defined.

In your case you could get away with it in you code by just adding in the start of your joined string and in the end, like the following, but i think my solution is more robust.

XmlDocument xd = new XmlDocument();
string s = "<Bugs>" + string.Join(",", BugWSResponseList2) + "</Bugs>";                    
xd.LoadXml(s);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...