I would like to convert the List to a xml document in c#.
我想将List转换为c#中的xml文档。
My code is as follows:
我的代码如下:
List<string> BugWSResponseList1 = new List<string>();
Logger.Write("\n\n" + DateTime.Now + " : " + " : START : Creation of a set of Bugs via bug.Add API");
BugWSResponseList1 = CreateBugs(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
Logger.Write("\n\n" + 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.
我一直在尝试将列表响应转换为字符串,然后尝试将其转换为xml文档,但是当我解析它时,它表明xml中有多个根元素。
The format of xml is as follows:
xml的格式如下:
<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:
请注意,它在xml中有两个不同的节点。到目前为止我正在做的是将List BugWSResponseList1转换为字符串,然后将其作为xml加载。这是我正在做的代码:
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.
但是当我这样做时,它说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
我想将List BugWSResponseList1转换为xml文档,因为我需要进一步解析它以执行代码。任何帮助,将不胜感激。谢谢
2 个解决方案
#1
3
You may use XElement or XDocument and LINQ. Providing a sample of what the XML should look like we could provide more info.
您可以使用XElement或XDocument和LINQ。提供XML应该是什么样子的样本我们可以提供更多信息。
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:
所以在我用你提供的两个xml加载列表之后我做了以下几点:
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.
我创建了一个新的XElement,它将保存XML的根。我将该根命名为Bugs。然后作为该根的子项,我将List的内容解析为XElement对象后。
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.
你缺少的是根元素。 XmlDocument需要定义一个根元素。
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);
#2
#1
3
You may use XElement or XDocument and LINQ. Providing a sample of what the XML should look like we could provide more info.
您可以使用XElement或XDocument和LINQ。提供XML应该是什么样子的样本我们可以提供更多信息。
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:
所以在我用你提供的两个xml加载列表之后我做了以下几点:
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.
我创建了一个新的XElement,它将保存XML的根。我将该根命名为Bugs。然后作为该根的子项,我将List的内容解析为XElement对象后。
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.
你缺少的是根元素。 XmlDocument需要定义一个根元素。
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);