Я работаю над веб-API MVC, где при одной функции мне нужно преобразовать результат SQL DB в файл XML.Geting XML в определенном формате C#
private string DbToXml(int fileID)
{
DataSet ds = new DataSet("Requirements");
string connetionString = ConfigurationManager.ConnectionStrings["DemoConnectionString"].ConnectionString;
string XML;
using (SqlConnection connection = new SqlConnection(connetionString))
{
string sql = "SELECT RequirementLabel as ID, VagueWord, Suggestion, Explanation, VaguePhrase, ContentText, VagueTypeText FROM [Test].[dbo].[Vague_Terms_View] where FileID=" + fileID;
string XML_Output_Path = System.Configuration.ConfigurationManager.AppSettings.Get("XML_Output_Path");
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(ds, "Requirement");
var sb = new StringBuilder();
XmlWriter xmlWrite = XmlWriter.Create(sb);
ds.WriteXml(xmlWrite);
XML = ds.GetXml();
}
Я получаю XML от этого кода, как показано ниже, который является правильным.
<?xml version="1.0" encoding="utf-8"?>
<Requirements>
<Requirement>
<ID>Req97</ID>
<VagueWord>or</VagueWord>
<Suggestion>Keep each requirement in a single sentence.</Suggestion>
<Explanation>Suggests that you are combining requirements. Requirements that contain conjunctions/disjunctions (AND/OR) are dangerous and can lead to downstream problems in defining scope of the requirement.</Explanation>
<VaguePhrase>Marketing or Servicing</VaguePhrase>
<ContentText>If a user is identified as Marketing or Servicing, then the Campaign Management (CM) hyperlink should be displayed.</ContentText>
<VagueTypeText>Not Standard</VagueTypeText>
</Requirement>
<Requirement>
<ID>Req97</ID>
<VagueWord>should</VagueWord>
<Suggestion>Use 'shall/must/will' for requirements,</Suggestion>
<Explanation>Is often ambiguous, or inappropriate. Some readers will interpret these as optional or advisory, others as required.</Explanation>
<ContentText>If a user is identified as Marketing or Servicing, then the Campaign Management (CM) hyperlink should be displayed.</ContentText>
<VagueTypeText>Not Standard</VagueTypeText>
</Requirement>
<Requirement>
<ID>Req98</ID>
<VagueWord>Unless</VagueWord>
<Suggestion>Specify each conditions explicitly. One condition per requirement.</Suggestion>
<Explanation>Is an escape clause. Requirements with escape clauses are not testable. The word implies additional condition to the requirement.</Explanation>
<ContentText>Unless Sleep, Latency, Noise, or apply conditions are present, the data transmissions will contain the code for Normal Operation.</ContentText>
<VagueTypeText>Not Standard</VagueTypeText>
</Requirement>
</Requirements>
Но теперь мне нужно преобразовать XML, например, проверить элемент ID внутри узла Требование. Если он повторяется в узлах ниже Требование, переименуйте его и все остальные элементы внутри узла Требование, чтобы добавить id = 1, и число будет продолжаться. Ожидаемый результат для XML выше, как показано ниже.
<?xml version="1.0" encoding="utf-8"?>
<Requirements>
<Requirement>
<ID "id=1">Req97</ID>
<VagueWord "id=1">or</VagueWord>
<Suggestion "id=1">Keep each requirement in a single sentence.</Suggestion>
<Explanation "id=1">Suggests that you are combining requirements. Requirements that contain conjunctions/disjunctions (AND/OR) are dangerous and can lead to downstream problems in defining scope of the requirement.</Explanation>
<VaguePhrase "id=1">Marketing or Servicing</VaguePhrase>
<ContentText "id=1">If a user is identified as Marketing or Servicing, then the Campaign Management (CM) hyperlink should be displayed.</ContentText>
<VagueTypeText "id=1">Not Standard</VagueTypeText>
</Requirement>
<Requirement>
<ID "id=2">Req97</ID>
<VagueWord "id=2">should</VagueWord>
<Suggestion "id=2">Use 'shall/must/will' for requirements,</Suggestion>
<Explanation "id=2">Is often ambiguous, or inappropriate. Some readers will interpret these as optional or advisory, others as required.</Explanation>
<ContentText "id=2">If a user is identified as Marketing or Servicing, then the Campaign Management (CM) hyperlink should be displayed.</ContentText>
<VagueTypeText "id=2">Not Standard</VagueTypeText>
</Requirement>
<Requirement>
<ID>Req98</ID>
<VagueWord>Unless</VagueWord>
<Suggestion>Specify each conditions explicitly. One condition per requirement.</Suggestion>
<Explanation>Is an escape clause. Requirements with escape clauses are not testable. The word implies additional condition to the requirement.</Explanation>
<ContentText>Unless Sleep, Latency, Noise, or apply conditions are present, the data transmissions will contain the code for Normal Operation.</ContentText>
<VagueTypeText>Not Standard</VagueTypeText>
</Requirement>
</Requirements>
@C четче, я думаю, нужно будет реализовать собственный сериализации. я пробовал в прошлом, и это непросто, и вы получите проблему с производительностью. Я попытаюсь найти свою долю кода вместе с вами. –
Значение атрибута 'id' должно быть в кавычках, чтобы сделать этот допустимый xml. Почему последние узлы не имеют атрибута 'id'? Если вы не хотите заполнять свой пример, просто оставьте его, чтобы сделать его непротиворечивым. – Filburt
@fabiosilvalima пожалуйста, поделитесь кодом, если у вас есть ... это было бы большой помощью .. !!! –