Xml doesn't work when i build

When i test my xml in unity program , it works so great . so i build a game . but when i try it with a game that i buid , it doesn’t work .

This is my script for write XML

			   using (XmlWriter writer = XmlWriter.Create("Assets/Data/sound.xml")) {
						writer.WriteStartElement ("sound");
							writer.WriteElementString ("SFX", "no");
							writer.WriteElementString ("MUSIC", "no");
							writer.WriteEndElement ();
				writer.Flush ();
				}

this is my script for read xml

           XmlDocument doc = new XmlDocument();

	doc.Load("Assets/Data/sound.xml");
	XmlNode root = doc.DocumentElement;
	XmlNode myNode = root.SelectSingleNode("SFX");
	
	if (myNode.InnerText == "no") {
					var shotTransform = Instantiate (x) as Transform;
					shotTransform.position = transform.position;
					Destroy (gameObject);
			}

i dont know why it doesn’t work with build version .

any idea?

it will not work on device,

for this u can use Resources.Load(“filename”);

but the file must be in Asset/Resources folder.

Try using the Application.DataPath instead:

Goodluck!

A nice method is to save the file in the Resources folder and then assign it to a TextAsset using the inspector. However, I don’t think you will be able to save to the Resources folder in build. If you need to save the file you should save it to application.PersistentDataPath.

[SerializeField]
private TextAsset
    xmlFile;

void Start() {
    XmlDocument doc = new XmlDocument();
    doc.Load(xmlFile.text);
}

[edit] In response to your comment.

It looks like you might be saving preferences to XML? Try changing these two lines:

// write
XmlWriter writer = XmlWriter.Create(Application.persistentDataPath + "/sound.xml");

//read
doc.Load(Application.persistentDataPath + "/sound.xml");