0

I have one string in format of XML, (this is not well-formatted XML!) and I would like to get the field and value

 <MYXML
address="rua sao carlos, 128" telefone= "1000-222" service="xxxxxx" source="xxxxxxx" username="aaaaaaa" password="122222" nome="asasas" sobrenome="aass" email="[email protected]" pais="SS" telefone="4002-" />  

I would like to get the parameter and value separeted in split.

I try this:

xml.ToString().Replace(" =" , "=").Replace("= " , "=").Replace(" = " , "=").Split(new char[]{' '});        

But not work perfect becase for example the attribute 'address' was split in two items

{string[29]}
[0]: "<signature"
[1]: "aaa=\"xxxx\""
[2]: "sss=\"xxxx\""
[3]: "ssss=\"xxx\""
[4]: "username=\"xxx\""
[5]: "password=\"xxxx\""
[6]: "nome=\"xxxx\""
[7]: "sobrenome=\"xxx\""
[8]: "email=\"[email protected]\""
[9]: "pais=\"BR\""
[10]: "endereco=\"Rua"
[11]: "Sao"
[12]: "Carlos,"
[13]: "128\""
[14]: "cidade=\"Sao"
[15]: "Paulo\""

The error is

   [10]: "endereco=\"Rua"
[11]: "Sao"
[12]: "Carlos,"      

When the correct I would like is

[10]: "endereco=\"Rua Sao Carlos , 128"
7
  • That is correct because the address is separtated by ' ' and u wanted to split with ' ' Commented Dec 17, 2013 at 22:52
  • Can you just rename the duplicate attribute and then parse as well-formatted XML? Commented Dec 17, 2013 at 22:55
  • Not becase this xml is diferent in some times. And not have one xml pattern,(one schemma) Commented Dec 17, 2013 at 22:56
  • So there are other reasons why the XML is not well-formed? Commented Dec 17, 2013 at 22:57
  • Yes, It is create from another company that's not know the 'schemma xml'. Commented Dec 17, 2013 at 22:58

4 Answers 4

1

A regular expression will work for this as you are working with badly formed xml.

        Regex regex = new Regex("\\s\\w+=\"(\\w|\\s|,|=|@|-|\\.)+\"");
        MatchCollection matches = regex.Matches(searchText);
        foreach (var match in matches)
        {
          //your code here
        }

Tested with your example string and matches were as expected. Hope this Helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Given that he is working with junk xml, this is the best choice. +1 Hope he can get good xml tho, then XDocument is the way to go.
thanks, and I agree that the best result would be to have good xml.
Hi, your solution it's work perfect AFrieze! I agree it's the best Approach! thank you very much!
Glad it helped! Mark as answer? :) (green checkbox)
0

I would suggest you to use xPath or Linq to parse this xml. The way you are using is not a good way and that is why you end up in error."Rua Sao Carlo" contains three words separated by single space ; as a result when you try to split it with single space, it also splits the address

2 Comments

There are multiple telefone attributes so it is not well-formed XML.
I know that is not well-formed . Becase this is a STRING and not have one "schema xml".
0

Try this overload of Split. It will allow you to use a string as the splitter token, namely '" ' (that is quote and space). This will split to the name and attribute pairs. Then take the resulting array, and split it again on = (equals) to get the pairs you need, then do as you will with them. Hope this gets you headed in the right direction

Comments

0

As already noted, you have badly formed XML. If you were to fix it, by either renaming or removing on of the telephone attributes, you could break down your XML like this:

This is the correct way to handle the XML, if however you do not have control over getting proper xml and must work w/ junk, i'd suggest the regex answer by @AFrieze.

var xmlString = @"<MYXML address=""rua sao carlos, 128"" service=""xxxxxx"" source=""xxxxxxx"" username=""aaaaaaa"" password=""122222"" nome=""asasas"" sobrenome=""aass"" email=""[email protected]"" pais=""SS"" telefone=""4002-"" />";
var xml = XDocument.Parse(xmlString);
var values = xml.Descendants("MYXML").SelectMany(x => x.Attributes()).ToArray();

foreach (var value in values)
{
    Console.WriteLine(value);
}
Console.Read();

This returns:

address="rua sao carlos, 128"
service="xxxxxx"
source="xxxxxxx"
username="aaaaaaa"
password="122222"
nome="asasas"
sobrenome="aass"
email="[email protected]"
pais="SS"
telefone="4002-"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.